s4553711
5/22/2017 - 2:46 PM

264.cpp

class Solution {
public:
    int minnum(int a,int b,int c){
        return c<(min(a,b))?c:min(a,b);
    }
    int nthUglyNumber(int n) {
        vector<int> result(1,1);
        int k2 = 0, k3 = 0, k5 = 0;
        while(result.size() < n) {
            int temp = minnum(result[k2]*2, result[k3]*3, result[k5]*5);
            result.push_back(temp);
            if (temp == result[k2]*2) {
                k2++;
            }
            if (temp == result[k3]*3) {
                k3++;
            }
            if (temp == result[k5]*5) {
                k5++;
            }
        }
        return result[n-1];
    }
};