剑指OFFER----面试题49. 丑数

链接:https://leetcode-cn.com/problems/chou-shu-lcof/

代码

class Solution {
public:
    int nthUglyNumber(int n) {
        vector<int> q(1, 1);
        int i = 0, j = 0, k = 0;
        while (--n) {
            int t = min(q[i] * 2, min(q[j] * 3, q[k] * 5));
            q.push_back(t);
            if (q[i] * 2 == t) i++;
            if (q[j] * 3 == t) j++;
            if (q[k] * 5 == t) k++;
        }
        return q.back();
    }
};
posted @ 2020-03-07 20:33  景云ⁿ  阅读(104)  评论(0编辑  收藏  举报