Leetcode之动态规划(DP)专题-264. 丑数 II(Ugly Number II)



编写一个程序,找出第 n 个丑数。

丑数就是只包含质因数 2, 3, 5 的正整数。

示例:

输入: n = 10
输出: 12
解释: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 是前 10 个丑数。

说明:

  1. 1 是丑数。
  2. n 不超过1690。


dp含义:

dp[i]表示第i-1个丑数。

class Solution {
    public int nthUglyNumber(int n) {
        int[] dp = new int[n + 1];
        dp[0] = 1;
        int i2 = 0;
        int i3 = 0;
        int i5 = 0;
        for (int i = 1; i < n + 1; i++) {
            dp[i] = Math.min(Math.min(dp[i2] * 2, dp[i3] * 3), dp[i5] * 5);
            if (dp[i] == dp[i2] * 2) {
                i2++;
            }
            if (dp[i] == dp[i3] * 3) {
                i3++;
            }
            if (dp[i] == dp[i5] * 5) {
                i5++;
            }
        }
        return dp[n - 1];
    }
}

原文地址:https://www.cnblogs.com/qinyuguan/p/11494532.html

时间: 2024-08-29 06:06:53

Leetcode之动态规划(DP)专题-264. 丑数 II(Ugly Number II)的相关文章

[Swift]LeetCode264.丑数 II | Ugly Number II

Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. Example: Input: n = 10 Output: 12 Explanation: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ugly numbers. Note

把只包含质因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含质因子7。 习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第N个丑数。

public class Solution { public int GetUglyNumber_Solution(int index) { if(index<=0){ return 0; } int[] p = new int[index]; p[0]=1; int p2=0; int p3=0; int p5=0; int num=1;//索引 while (num < index){ int min = Min(p[p2]*2,p[p3]*3,p[p5]*5); p[num] = min

LeetCode(264):Ugly Number II

Ugly Number II:Write a program to find the n-th ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ugly numbers.Note that 1 is typically

leetcode Ugly Number II

题目连接 https://leetcode.com/problems/ugly-number-ii/ Ugly Number II Description Write a program to find the $n_{th}$ ugly number. Ugly numbers are positive numbers whose prime factors only include $2, 3, 5.$ For example,$ 1, 2, 3, 4, 5, 6, 8, 9, 10, 12

263. Ugly Number &amp;&amp; 264. Ugly Number II &amp;&amp; 313. Super Ugly Number

263. Ugly Number Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7.

LeetCode——264. 丑数 II

编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n = 10 输出: 12 解释: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 是前 10 个丑数. 说明: 1 是丑数. n 不超过1690. 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/ugly-number-ii 1.暴力(brute force) class Solution { public: int nth

Leetcode 264.丑数II

丑数II 编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n = 10 输出: 12 解释: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 是前 10 个丑数. 说明:  1 是丑数. n 不超过1690. 思路:动态规划思想.后面的丑数一定是由前面的丑数乘以2.3或5得到.所以第n个丑数一定是由前n-1个数中的某3个丑数(分别记为index2.index3.index5)分别乘以2.3或者5得到的数中的最小数,index2,

[LeetCode]70. Ugly Number II第N个丑数

Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ugly numbers. Note that 1 is typically treated as an

264丑数II

题目: 来源: 法一:自己的超时代码 思路:从2开始由小到大遍历判断每一个数是否为丑数,由于到后面丑数越来越稀疏,导致非常费时间. class Solution: def nthUglyNumber(self, n: int) -> int: if n == 1: return 1 ans = [1] k = 2 while len(ans) < n: for j in [2,3,5]: # 如果除以2 3 5中的某个数可以除尽,则判断商是否在之前的数中 # 如果在说明是丑数,否则不是 if