[转Medium]264. Ugly Number II

Ugly Number II

每次都找最小丑数,思路是从1开始,每次都乘以2,3,5

class Solution {
public:
    int nthUglyNumber(int n) {
        vector res(1, 1);//表示定义1行1列的vector数组
        int i2 = 0, i3 = 0, i5 = 0;
        while (res.size() < n) {
            int m2 = res[i2] * 2, m3 = res[i3] * 3, m5 = res[i5] * 5;
            int mn = min(m2, min(m3, m5));
            if (mn == m2) ++i2;
            if (mn == m3) ++i3;
            if (mn == m5) ++i5;
            res.push_back(mn);
        }
        return res.back();
    }
};

你可能感兴趣的:([转Medium]264. Ugly Number II)