关闭

LeetCode 264 Ugly Number II (递推)

标签: leetcode丑数2
406人阅读 评论(0) 收藏 举报
分类:

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 ugly number.


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

题目分析:用一个uglyNum数组记录,保证按升序构造,用当前的每个丑数,分别乘2,3,5其中比最大的大的最少的就是当前的下一个丑数,递推时间复杂度O(n)

public class Solution {
    
    public int Min3(int a, int b, int c) {
        return Math.min(a, Math.min(b, c));
    }
    
    public int nthUglyNumber(int n) {
        int[] uglyNum = new int[n + 5];
        uglyNum[1] = 1;
        int i = 1, i2 = 1, i3 = 1, i5 = 1, cur;
        while(i <= n) {
            cur = Min3(uglyNum[i2] * 2, uglyNum[i3] * 3, uglyNum[i5] * 5);
            if(cur == uglyNum[i2] * 2) {
                i2 ++;
            }
            if(cur == uglyNum[i3] * 3) {
                i3 ++;
            }
            if(cur == uglyNum[i5] * 5) {
                i5 ++;
            }
            uglyNum[++ i] = cur;
        }
        return uglyNum[n];
    }
}


0
0
查看评论

[Leetcode 264] 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,&...
  • sbitswc
  • sbitswc
  • 2015-09-02 00:23
  • 2226

[leetcode] 264. Ugly Number II 解题报告

题目链接:https://leetcode.com/problems/ugly-number-ii/ Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime...
  • qq508618087
  • qq508618087
  • 2015-12-13 15:54
  • 518

[leetcode-264]Ugly Number II(java)

问题描述: 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, ...
  • zdavb
  • zdavb
  • 2015-08-24 11:12
  • 1142

leetcode 264: Ugly Number II

Ugly Number II Total Accepted: 2920 Total Submissions: 15174 Write a program to find the n-th ugly number. Ugly numbers are positive numb...
  • xudli
  • xudli
  • 2015-08-23 10:53
  • 5264

[C++]Ugly Number II丑数2

leetcode 原题链接:https://leetcode.com/problems/ugly-number-ii/ Write a program to find the n-th ugly number. Ugly numbers are positive numbers wh...
  • lyy_hit
  • lyy_hit
  • 2015-08-19 15:51
  • 1024

leetcode笔记:Ugly Number II

从1开始的丑数为:1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, … 该题的大意是,输入一个正整数n,返回第n个丑数。
  • liyuefeilong
  • liyuefeilong
  • 2016-01-10 18:42
  • 1553

Ugly Number II(求第N个丑数)

这个应该是微软的一道面试题,首先需要明白丑数的概念。1是一个特殊的丑数,其次只含有2,3,5质因数的数被定义为丑数。 关于解题的思路可以参考: http://www.geeksforgeeks.org/ugly-numbers/ 1到N的丑数为 1, 2, 3, 4, 5, 6, 8, 9, ...
  • guang09080908
  • guang09080908
  • 2015-08-19 14:49
  • 5405

LeetCode 264: Ugly Number II

Ugly Number IIWrite 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,...
  • yongyuandeie
  • yongyuandeie
  • 2015-10-29 15:18
  • 130

LeetCode(264) Ugly Number II

不知道为什么注释掉的三条if语句不和下面的两条语句等价。一个导致结果是正确的,一个导致是错误的,想不通。class Solution { public: int nthUglyNumber(int n) { int id2 = 0; int id3 = 0; int id5 =...
  • guanzhongshan
  • guanzhongshan
  • 2015-08-30 09:39
  • 398

leetCode #264 Ugly Number II

题目:输出第n个丑数(关于丑数的定义见ugly number) 分析:这就需要生成n个丑数。我们可以看到,1是丑数,然后其2,3,5倍皆是丑数。这要求我们将2,3,5的幂都排序,可以用优先队列来做,也可以依大小生成:每次生成的是乘以2,3,5中最小的,然后乘过的做一个标记。 答案: class...
  • MarStarck
  • MarStarck
  • 2015-08-22 17:00
  • 213
    个人资料
    • 访问:741272次
    • 积分:15318
    • 等级:
    • 排名:第879名
    • 原创:818篇
    • 转载:7篇
    • 译文:0篇
    • 评论:122条
    博客专栏