丑数是求取


Example:

Input: n = 10
Output: 12
Explanation: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ugly numbers.

class Solution {

public:
    int nthUglyNumber(int n) {
        
        int t2=0,t3=0,t5=0; // 能被2,3,5整除的最小下标
        vector<int> ivec(n,0);
        ivec[0]=1;
        for(int i=1;i<n;++i){
            int temp=min(ivec[t2]*2,ivec[t3]*3);
            ivec[i]=min(temp,ivec[t5]*5);
            
            if(ivec[t2]*2==ivec[i]){
                t2++;
            }
            if(ivec[t3]*3==ivec[i]){
                t3++;
            }
            if(ivec[t5]*5==ivec[i]){
                t5++;
            }
        }
        return ivec[n-1];
    }
};

猜你喜欢

转载自blog.csdn.net/u010325193/article/details/80358623
今日推荐