zoukankan      html  css  js  c++  java
  • Leetcode264. Ugly Number II丑数2

    编写一个程序,找出第 n 个丑数。

    丑数就是只包含质因数 2, 3, 5 的正整数。

    示例:

    输入: n = 10 输出: 12 解释: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 是前 10 个丑数。

    说明:  

    1. 1 是丑数。
    2. n 不超过1690。

    不用else if的原因是为了去重

    class Solution {
    public:
        int nthUglyNumber(int n) 
        {
            int three = 0;
            int two = 0;
            int five = 0;
            vector<int> res(n, 0);
            res[0] = 1;
            for(int i = 1; i < n; i++)
            {
                res[i] = min(res[two] * 2, min(res[three] * 3, res[five] * 5));
                if(res[i] == res[two] * 2)
                {
                    two++;
                }
                if(res[i] == res[three] * 3)
                {
                    three++;
                }
                if(res[i] == res[five] * 5)
                {
                    five++;
                }
            }
            return res[n - 1];
        }
    };
  • 相关阅读:
    LTE
    LTE
    LTE
    LTE
    LTE DL-SCH and PDSCH Processing Chain
    LTE PDSCH Port 5 UE-Specific Beamforming
    推荐我的公众号
    GitHub Top 微信小程序
    深度前馈网络
    考研经历吐血总结
  • 原文地址:https://www.cnblogs.com/lMonster81/p/10433816.html
Copyright © 2011-2022 走看看