Ugly Number II

原创 2016年05月30日 21:38:12
  • 题目: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.
    解题思路:
    1.若利用回调函数一次计算每个丑数,最后输出第n个丑数,那么会出现“计算时间超出”的错误,相对而言,丑数的数目更小,所以集中分析丑数的计算;
    2.一个丑数必然是2,3或5的倍数(1除外);
    3.关键是如何保证丑数次序,无遗漏地列举所有丑数;采用三个丑数合并的方式一次求解每组丑数,每组较大的丑数[L1,L2,L3]都是由较小的丑数的2倍,3倍或者5倍求出;(Assume you have Uk, the kth ugly number. Then Uk+1 must be Min(L1 * 2, L2 * 3, L3 * 5).)
    JAVA解调思路:
public class Solution {
    public int nthUglyNumber(int n) {
        if(n==1)
        {
            return 1;
        }
        int[] uglyNumber=new int[n];//用于保存ugly Number的数组
        int[] index={0,0,0};     //保存L1,L2,L3的值
        int[] tempNum={2,3,5};  //保存2*L1,3*L2,5*L3的值
        int   tempIndex=0;  //uglyNumber数组的下标值
        int   sortIndex=0;  //保存带插入的index值
        int   min=0;   
        //变量初始化
        uglyNumber[0]=1;
        while(tempIndex<(n-1))
        {
             tempIndex++;
             min=minUglyNumber(tempNum);//取得最小数字的index
             uglyNumber[tempIndex]=min;//将最小的数添加至uglyNumber数组
            //修改下标数组对应值,此处注意此处相同的值下标要一起增加
            if(tempNum[0]==min)
            {
                index[0]++;
                tempNum[0]=2*uglyNumber[index[0]];

            }
            if(tempNum[1]==min)
            {
                 index[1]++;
                tempNum[1]=3*uglyNumber[index[1]];
            }
            if(tempNum[2]==min)
            {
                 index[2]++;
                tempNum[2]=5*uglyNumber[index[2]];
            }
        }
        return min;
    }
    public int minUglyNumber(int[] num)
    {
        int temp=num[0]<num[1]?num[0]:num[1];
        return temp<num[2]?temp:num[2];
    }
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

264. Ugly Number II 类别:动态规划 难度:medium

题目: Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose pr...

264. Ugly Number II

Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors...
  • x_shuck
  • x_shuck
  • 2016年05月16日 18:37
  • 139

LeetCode Ugly Number i,ii详解

// Date : 2016.07.25 // Author : yqtao // https://github.com/yqtaowhu/**************************...

[Leetcode] Ugly Number II

题目描述: Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose pr...

LeetCode-263.264. Ugly Number II (JAVA)丑数

LeetCode-263.264. Ugly Number II (JAVA)丑数

leetcode Ugly Number II

Problem Write a program to find the n-th ugly number. Ugly numbers are positive numbers whos...

Leetcode 264(Ugly Number II)

QuestionWrite a program to find the nthn^{th} ugly number.Ugly numbers are positive numbers whose pr...
  • senliuy
  • senliuy
  • 2015年08月19日 13:31
  • 516

<LeetCode OJ> 26 / 264 / 313 Ugly Number (I / II / III)

263. Ugly Number My Submissions Question Total Accepted: 33755 Total Submissions: 96873 Difficul...

LeetCode 264: Ugly Number II

264. Ugly Number IIDifficulty: Medium Write a program to find the n-th ugly number.Ugly numbers are...

LeetCode (39) Ugly Number I II (丑数)

Ugly Number I 和 Ugly Number II
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Ugly Number II
举报原因:
原因补充:

(最多只允许输入30个字)