LeetCode OJ:Ugly Number II(丑数II)

时间:2023-12-27 12:13:37

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.

丑数问题,这一第n个应该按照顺序来数,这样可以使用一个set来维护顺序,直到数到第n个丑数:

 class Solution {
public:
int nthUglyNumber(int n) {
set<long> ret;
long res;
ret.insert();
for(int i = ; i < n; ++i){
res = *ret.begin();
ret.insert(res * );
ret.insert(res * );
ret.insert(res * );
ret.erase(res);
}
return (int)res;
}
};