题目描述
把只包含因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含因子7。 习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第N个丑数。
class Solution {
public:
int GetUglyNumber_Solution(int index) {
if(index <= 0)return 0;
vector<int>res;
res.push_back(1);
int t2,t3,t5;
t2 = t3 = t5 =0;
for(int i = 1; i < index; i++){
int m = min(min(res[t2] * 2,res[t3] * 3),res[t5] * 5);
res.push_back(m);
if(m == res[t2] * 2)t2++;
if(m == res[t3] * 3)t3++;
if(m == res[t5] * 5)t5++;
}
return res[index - 1];
}
};