264. Ugly Number II
problem description
Input: n = 10
Output: 12
Explanation: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ugly numbers.algorithm thought
code
class Solution {
public:
int nthUglyNumber(int n) {
if(n<=1)
return 1;
vector<int> res(n,1);
int t1=0,t2=0,t3=0;
for(int i=1;i<n;++i){
res[i]=min(res[t1]*2,min(res[t2]*3,res[t3]*5));
if(res[t1]*2==res[i])
t1++;
if(res[t2]*3==res[i])
t2++;
if(res[t3]*5==res[i])
t3++;
}
return res.back();
}
};algorithm analysis
Last updated