一、题目地址
https://leetcode-cn.com/problems/ugly-number-ii/
二、具体代码
/**
* @param {number} n
* @return {number}
*/
//动态规划做法
//时间复杂度:O(N)
//空间复杂度:O(N)
var nthUglyNumber = function(n) {
if(n < 0) {
return 0;
}
let uglyArr = new Array(n);
uglyArr[0] = 1;
let p2 = 0, p3 = 0, p5 = 0;
for(let i=1; i<n; i++) {
let num1 = uglyArr[p2] * 2;
let num2 = uglyArr[p3] * 3;
let num3 = uglyArr[p5] * 5;
uglyArr[i] = Math.min(num1, num2, num3);
if(num1 === uglyArr[i]) {
p2++;
}
if(num2 === uglyArr[i]) {
p3++;
}
if(num3 === uglyArr[i]) {
p5++;
}
}
return uglyArr[n-1];
};
三、补充链接
https://b23.tv/UL7wJWP
https://leetcode-cn.com/problems/ugly-number-ii/solution/chou-shu-ii-by-leetcode-solution-uoqd/