class Solution {
public:
int nthUglyNumber(int n) {
int cnt = 1;
vector<int> dp(n,0);
if(n == 1){
return 1;
}
/*index of the merge sort*/
int index2 = 0;
int index3 = 0;
int index5 = 0;
int factor2 = 2;
int factor3 = 3;
int factor5 = 5;
/**/
int number = 0;
dp[0] = 1;
for(int i = 1;i < n ; ++i){
number = min(factor2,min(factor3,factor5));
dp[i] = number;
if(dp[i] == factor2){
index2++;
factor2 = 2*dp[index2];
}
if(dp[i] == factor3){
index3++;
factor3 = 3*dp[index3];
}
if(dp[i] == factor5){
index5++;
factor5 = 5*dp[index5];
}
//cout<<dp[i]<<",";
}
return dp[n-1];
}
};
264. Ugly Number II
最新推荐文章于 2022-01-07 04:17:23 发布