leetcode 263&264: Ugly Number I & II

此题在《剑指offer》中面试题34也有讲解。

Ugly Number I :easy

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.

Note that 1 is typically treated as an ugly number.

思路:

1.输入时整数,要求是positive number,所以对输入的判断是是否为正数。返回值类型为boolean类型。

2.依次判断是否可以被2,3,5整除。

代码:

public class Solution {
    public boolean isUgly(int num) {
        if(num<=0) return false;
        while(num!=1){
            if(num%2==0) num = num/2;
            else if(num%3==0) num = num/3;
            else if(num%5==0) num = num/5;
            else return false;
        }
        return true;
    }
}


Ugly Number II :medium

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.

Hint:

  1. The naive approach is to call isUgly for every number until you reach the nth one. Most numbers are not ugly. Try to focus your effort on generating only the ugly ones.
  2. An ugly number must be multiplied by either 2, 3, or 5 from a smaller ugly number.
  3. The key is how to maintain the order of the ugly numbers. Try a similar approach of merging from three sorted lists: L1, L2, and L3.
  4. Assume you have Uk, the kth ugly number. Then Uk+1 must be Min(L1 * 2, L2 * 3, L3 * 5).
思路:

1.简单利用ugly number的判断方法,不考虑时间复杂度可以得到解法,算法直观代码简单,但是所有的整数都需要进行计算,时间效率低。

  显示超时,我用eclipse计算了一下输入1352的时间,大概是34秒。

long starTime=System.currentTimeMillis();
long endTime=System.currentTimeMillis();
long Time=endTime-starTime;

代码:

public class Solution {
    public int nthUglyNumber(int n) {
        int count=0;
        int nth=0;
        for(int i=1;count<n;i++){
            nth=i;
            int tmp=i;
            while(tmp!=1){
                if(tmp%2==0) tmp=tmp/2;
                else if(tmp%3==0) tmp=tmp/3;
                else if(tmp%5==0) tmp=tmp/5;
                else break;
            }
            if(tmp==1) ++count;
        }
        return nth;
    }
}
2.根据丑数的定义,丑数应该另一个丑数乘以2,3,5的结果(除1),这样避免了计算所有的整数。定义三个list,每个list中分别存放2,3,5的倍数,最开始都放入1,然后找到最小的数字,把它的2倍,3倍,5倍分别放到三个表中,依次。要注意的是,把最小的剔除,避免重复的计算。

代码:

public class Solution {  
    public int nthUglyNumber(int n) {  
        int nth = 0;  
        List<Integer> l1 = new LinkedList<Integer>();  
        List<Integer> l2 = new LinkedList<Integer>();  
        List<Integer> l3 = new LinkedList<Integer>();  
        l1.add(1);  
        l2.add(1);  
        l3.add(1);  
          
        for(int i=0; i<n; i++) {  
            nth = Math.min( Math.min(l1.get(0), l2.get(0)), l3.get(0));  
              
            if(l1.get(0) == nth) l1.remove(0);  
            if(l2.get(0) == nth) l2.remove(0);  
            if(l3.get(0) == nth) l3.remove(0);  
              
            l1.add(nth*2);  
            l2.add(nth*3);  
            l3.add(nth*5);  
        }  
        return nth;  
    }  
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值