LintCode:丑数 II

设计一个算法,找出只含素因子235 的第 n 大的数。

符合条件的数如:1, 2, 3, 4, 5, 6, 8, 9, 10, 12...

 注意事项

我们可以认为1也是一个丑数

样例

如果n = 9, 返回 10


class Solution {
public:
    /*
     * @param n an integer
     * @return the nth prime number as description.
     */
    int nthUglyNumber(int n) {
        // write your code here
        int count = 1;
        vector<int> ugly(n,0);
        ugly[0] = 1;
        vector<int> idx(3,0);
        while(count < n)
        {
            int a = 2 * ugly[idx[0]];
            int b = 3 * ugly[idx[1]];
            int c = 5 * ugly[idx[2]];
            
            int temp = min(min(a, b), c);
            if(temp == a)
              ++idx[0];
            if(temp == b)
              ++idx[1];
            if(temp == c)
              ++idx[2];
            ugly[count] = temp;
            ++count;
        }
        return ugly[n-1];
    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值