[LeetCode]264. Ugly Number II
程序员文章站
2022-04-24 15:34:52
...
[LeetCode]264. Ugly Number II
题目描述
思路
动态规划,题目313的简单版
题目313看这里
[LeetCode]313. Super Ugly Number
代码
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
class Solution {
public:
int nthUglyNumber(int n) {
vector<int> primes = { 2,3,5 }, indexArr = { 0,0,0 }, uglyArr(n, INT_MAX);
uglyArr[0] = 1;
for (int i = 1; i < n; ++i) {
for (int index = 0; index < 3; index++) uglyArr[i] = min(uglyArr[i], uglyArr[indexArr[index]] * primes[index]);
for (int index = 0; index < 3; index++) indexArr[index] += (uglyArr[i] == uglyArr[indexArr[index]] * primes[index]);
}
return uglyArr[n - 1];
}
};
int main() {
Solution s;
cout << s.nthUglyNumber(10) << endl;
system("pause");
return 0;
}
推荐阅读
-
LeetCode 204. 计数质数 263. 丑数 264. 丑数 II
-
Leetcode练习 #264 Ugly Number II
-
(M)Dynamic Programming:264. Ugly Number II
-
[LeetCode]264. Ugly Number II
-
264. Ugly Number II (M)
-
264. Ugly Number II (python+cpp)
-
LeetCode 264. Ugly Number II(丑数II)
-
Ugly Number II
-
Leetcode 263. Ugly Number(python+cpp)
-
[LeetCode]313. Super Ugly Number