题目:
Write a program to find the
n
-th ugly number.Ugly numbers are positive numbers whose prime factors only include
2, 3, 5
.
Example:
Input: n = 10
Output: 12
Explanation: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ugly numbers.
Note:
1
is typically treated as an ugly number.n
does not exceed 1690.
解析:
The ugly-number sequence is 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, …
because every number can only be divided by 2, 3, 5, one way to look at the sequence is to split the sequence to three groups as below:
(1) 1×2, 2×2, 3×2, 4×2, 5×2, …
(2) 1×3, 2×3, 3×3, 4×3, 5×3, …
(3) 1×5, 2×5, 3×5, 4×5, 5×5, …
We can find that every subsequence is the ugly-sequence itself (1, 2, 3, 4, 5, …) multiply 2, 3, 5.
Then we use similar merge method as merge sort, to get every ugly number from the three subsequence.
Every step we choose the smallest one, and move one step after,including nums with same value.
代码:
class Solution {
public:
int nthUglyNumber(int n) {
vector<int> res(1,1);
if(n == 1)
return 1;
int i = 0;
int j = 0;
int k = 0;
while(res.size() < n)
{
int next = min(res[i]*2, min(res[j]*3, res[k]*5));
if(next == res[i]*2) ++i;
if(next == res[j]*3) ++j;
if(next == res[k]*5) ++k;
res.push_back(next);
}
return res.back();
}
};