Leetcode 264. Ugly Number II

来源:互联网 发布:好心分手 知乎 编辑:程序博客网 时间:2024/06/01 19:21

Write a program to find the n-th ugly number.

Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ugly numbers.

Note that 1 is typically treated as an ugly number, and n does not exceed 1690.

找到第n个丑陋数。

三个下标打表。

class Solution {public:    int nthUglyNumber(int n) {        vector<int> res(1, 1);        int i = 0, j = 0, k = 0;        while(res.size() < n)        {            res.push_back(min(min(res[i] * 2, res[j] * 3), res[k] * 5));            if(res.back() == res[i] * 2) i++;            if(res.back() == res[j] * 3) j++;            if(res.back() == res[k] * 5) k++;        }        return res.back();    }};


1 0