欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

[LeetCode]264. Ugly Number II

程序员文章站 2022-04-24 15:34:52
...

[LeetCode]264. Ugly Number II

题目描述

[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