丑数(由质因子生成数)
丑数
http://www.nowcoder.com/questionTerminal/6aa9e04fc3794f68acf8778237ba065b
只包含质因子2,3,5。
此数表示为:
判断数的大小值依次递增x,y,z的值。
class Solution {
public:
int GetUglyNumber_Solution(int index) {
if(index <= 0)return 0;
vector<int> res(index);
res[0] = 1;
int p2=0,p3=0,p5=0;
for(int i = 1; i < index; ++i){
int s = min(res[p2] * 2,min(res[p3] * 3, res[p5] * 5));
if(s == res[p2] * 2) p2++;
if(s == res[p3] * 3) p3++;
if(s == res[p5] * 5) p5++;
res[i] = s;
}
return res[index-1];
}
};
查看14道真题和解析