Ugly Number I and II

/*
* 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.
*/
public boolean nthSuperUglyNumber1(int num) {
/*if(num!=1){
while(num%2==0)
num=num/2;
if(num == 1)return true;
else{
while(num%3==0)
num=num/3;
if(num == 1)return true;
else{
while(num%5==0)
num=num/5;
if(num == 1)return true;
else return false;
}
}
}
return false;*/

if(num<=0) return false;  
        if(num==1) return true;  
          
        while(num>=2 && num%2==0) num/=2;  
        while(num>=3 && num%3==0) num/=3;  
        while(num>=5 && num%5==0) num/=5;  
          
        return num==1;  
 

}
/*
 * Write a program to find the nth super ugly number.


Super ugly numbers are positive numbers whose all prime factors are in the given prime list primes of size k. 
For example, [1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32] is the sequence of the first 12 
super ugly numbers given primes = [2, 7, 13, 19] of size 4.


Note:
(1) 1 is a super ugly number for any given primes.
(2) The given numbers in primes are in ascending order.
(3) 0 < k ≤ 100, 0 < n ≤ 106, 0 < primes[i] < 1000.
 */
/*
* 超级丑数。跟丑数II很类似,只不过这次primes从2, 3, 5变成了一个size k的list。方法应该有几种,一种是维护一个size K的
* min-oriented heap,heap里是k个queue,和Ugly Number II的方法一样,取最小的那一个,然后更新其他Queue里的元素,
* n--,最后n = 1时循环结束。  另外一种是类似dynamic programming的方法,主要参考了larrywant2014大神的代码。
* 维护一个index数组,维护一个dp数组。每次遍历更新的转移方程非常巧妙,min = dp[[index[j]]] * primes[j]。
* 之后再便利一次primes来update每个数在index[]中的使用次数。
*/
public int nthSuperUglyNumber(int n, int[] primes) {
int len=primes.length;
int[] res= new int[n+1];//村结果
int[] index=new int[len];//存因子
res[0]=1;
if(n<=1)return res[0];
for(int i=1;i<n;i++){
int num=Integer.MAX_VALUE;
for(int j=0;j<len;j++)
num=Math.min(num, primes[j]*res[index[j]]);
res[i]=num;
for(int j=0;j<len;j++)
if(res[i]%primes[j]==0)index[j]++;
}
        return res[n-1];
    }


/*
 * 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个丑数


说明:丑数具有如下特征:1是丑数,丑数可以表示为有限个2、3、5的乘积
关于如果更加快速有效地找出第n个丑数的问题,网上已经有很多解题方法和大牛的博客可以参考,这里只是简要说明我用的方法:


申请一个长度为n的数组uglyNumbers,用于从小到大顺序存储n个丑数,数组中的首项为1,即第一个丑数为1


设置三个变量idx2、idx3、idx5存储下标,初始值都为0


找出数组uglyNumbers[idx2]*2、uglyNumbers[idx3]*3、uglyNumbers[idx5]*5的最小值,最小值即为下一个丑数,同时更新最小值对应的下标,如果多个数字同时为最小值,则它们的下标都要更新


找到第n个丑数时,循环结束
 */


public int nthUglyNumber(int n) {
if(n<0)return 0;
int[] nums=new int[n+1];
int index2,index3,index5;
index2=1;
index3=1;
index5=1;
nums[0]=0;
nums[1]=1;
for(int i=2;i<=n;i++){
int min=minOfRange(nums[index2],nums[index3],nums[index5]);

if(min==nums[index2]*2){

index2++;
}
if(min==nums[index3]*3){
index3++;
}if(min==nums[index5]*5){
index5++;
}
nums[i]=min;
System.out.println(nums[i]);
System.out.println("index2:"+index2+"\t"+"index3:"+index3+"\t"+"index5:"+index5+"\t");
}
return nums[n];
    
}
private int minOfRange(int index2, int index3, int index5) {
// TODO Auto-generated method stub
int min=index2*2<index3*3?index2*2:index3*3;

return min<index5*5?min:index5*5;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值