给你一个整数 n ,请你找出并返回第 n 个 丑数 。
丑数 就是只包含质因数 2、3 和/或 5 的正整数。
示例 1:
输入:n = 10
输出:12
解释:[1, 2, 3, 4, 5, 6, 8, 9, 10, 12] 是由前 10 个丑数组成的序列。
示例 2:
输入:n = 1
输出:1
解释:1 通常被视为丑数。
提示:
1 <= n <= 1690
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/ugly-number-ii
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
104 我用devC++跑的1800 leetcode上显示我的输出是1728,就奇奇怪怪,码住。
class Solution {
public:
int nthUglyNumber(int n) {
set<int> s;
if(n == 1) return 1;
s.insert(1);
while(--n){
int val = *s.begin();
if(val>1690) continue;
s.insert(val*2);
s.insert(val*3);
s.insert(val*5);
s.erase(val);
/* cout << val << endl;
set<int>::iterator it; //定义前向迭代器
for(it=s.begin();it!=s.end();it++)
cout<<*it<< " ";
cout << endl; */
}
return *s.begin();
}
};