前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >剑指offer——丑数

剑指offer——丑数

作者头像
AI那点小事
发布2020-04-18 00:50:14
3470
发布2020-04-18 00:50:14
举报

概述

题目描述 把只包含质因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含质因子7。 习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第N个丑数。


C++ AC代码

#include <iostream>
#include <cmath>
#include <vector> 
using namespace std;

class Solution {
    public:
        int GetUglyNumber_Solution(int index) {
            if(index < 7){
                return index;
            }
            vector<int> ans(index);
            ans[0] = 1;
            int t2 = 0,t3 = 0,t5 = 0;
            for(int i = 1 ; i < index ; i++){
                ans[i] = min(ans[t2]*2,min(ans[t3]*3,ans[t5]*5));
                if(ans[t2]*2 == ans[i]){
                    t2++;
                }
                if(ans[t3]*3 == ans[i]){
                    t3++;
                }
                if(ans[t5]*5 == ans[i]){
                    t5++;
                }
            }
            return ans[index-1];
        }
};

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 概述
  • C++ AC代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档