丑数
把只包含因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含因子7。 习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第N个丑数。
1 class Solution { 2 public: 3 int GetUglyNumber_Solution(int index) { 4 if(index==0) return 0; 5 vector<int> ugly; 6 ugly.push_back(1); 7 int uglyindex=1; 8 int count2=0,count3=0,count5=0; 9 while(uglyindex<index){ 10 int tmp=min(ugly[count2]*2,ugly[count3]*3); 11 tmp=min(tmp,ugly[count5]*5); 12 ugly.push_back(tmp); 13 uglyindex++; 14 while(ugly[count2]*2<=tmp) 15 count2++; 16 while(ugly[count3]*3<=tmp) 17 count3++; 18 while(ugly[count5]*5<=tmp) 19 count5++; 20 21 } 22 return ugly[uglyindex-1]; 23 } 24 };
联系方式:emhhbmdfbGlhbmcxOTkxQDEyNi5jb20=