码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode(264):Ugly Number II

时间:2016-03-05 20:31:38      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:

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 treated as an ugly number.

题意:找出第n个丑数。

代码:

public class Solution {
    public int nthUglyNumber(int n) {
        int[] uglyNums = new int[n];
        int next = 1;
        int i=0,j=0,k=0;
        uglyNums[0] = 1;
        while(next<n){
            int min = MinT(uglyNums[i]*2,uglyNums[j]*3,uglyNums[k]*5);
            uglyNums[next] = min;
                if(uglyNums[i]*2 == min) i++;
            if(uglyNums[j]*3 == min) j++;
            if(uglyNums[k]*5 <= min) k++;
            next++;
        }
        return uglyNums[n-1];
    }
    public int MinT(int t1,int t2,int t3){
        int min = t1<t2?t1:t2;
        return min<t3?min:t3;
        
    }
}

LeetCode(264):Ugly Number II

标签:

原文地址:http://www.cnblogs.com/Lewisr/p/5245680.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!