题解 | #丑数#
丑数
https://www.nowcoder.com/practice/6aa9e04fc3794f68acf8778237ba065b
function GetUglyNumber_Solution(index)
{
if (index === 0) return 0
// write code here
const cache = new Array(1)
cache[0] = 1
let i = 0
let j = 0
let k = 0
while (cache.length < index) {
const now = Math.min(cache[i] * 2, cache[j] * 3, cache[k] * 5)
cache.push(now)
if (now === cache[i] * 2) {
i++
}
if (now === cache[j] * 3) {
j++
}
if (now === cache[k] * 5) {
k++
}
}
return cache[index - 1]
}
module.exports = {
GetUglyNumber_Solution : GetUglyNumber_Solution
};
海康威视公司福利 1330人发布