其他分享
首页 > 其他分享> > 丑数II

丑数II

作者:互联网

题源:LeetCode

链接:https://leetcode-cn.com/problems/ugly-number-ii/

 

 

 

考虑使用dp

 1 class Solution {
 2 public:
 3     int nthUglyNumber(int n) {
 4         vector<int> dp(n + 1);
 5         dp[1] = 1;
 6         int p2 = 1, p3 = 1, p5 = 1;
 7         for (int i = 2; i <= n; i++) {
 8             int num2 = dp[p2] * 2, num3 = dp[p3] * 3, num5 = dp[p5] * 5;
 9             dp[i] = min(min(num2, num3), num5);
10             if (dp[i] == num2) {
11                 p2++;
12             }
13             if (dp[i] == num3) {
14                 p3++;
15             }
16             if (dp[i] == num5) {
17                 p5++;
18             }
19         }
20         return dp[n];
21     }
22 };

 

标签:丑数,cn,int,number,II,ii,dp
来源: https://www.cnblogs.com/hosheazhang/p/15147338.html