leetcode-华为专题-剑指 Offer 49. 丑数

 

 

class Solution {
public:
    int nthUglyNumber(int n) {
        vector<int> res(n,1);

        int a = 0;
        int b = 0;
        int c = 0;
        
        for(int i = 1; i < n; i++){
            // 定义三个指针分别维护 2 3 5
            int temp = min(min(res[a]*2,res[b]*3),res[c]*5);
            res[i] = temp;
            if(temp == res[a]*2)
                a++;
            if(temp == res[b]*3)
                b++;
            if(temp == res[c]*5)
                c++;

        }
        return res[n-1];
    }
};

 

posted @ 2021-08-14 17:13  三一一一317  阅读(36)  评论(0)    收藏  举报