丑数(LintCode)

丑数
设计一个算法,找出只含素因子357 的第 k大的数。

符合条件的数如:3,5,7,9,15......

样例

如果k=4, 返回 9

挑战

要求时间复杂度为O(nlogn)或者O(n)

 

 1 import java.util.Queue;
 2 import java.util.LinkedList;
 3 
 4 class Solution {
 5     /**
 6      * @param k: The number k.
 7      * @return: The kth prime number as description.
 8      */
 9     public long kthPrimeNumber(int k) {
10         long[] relust = new long[k];
11         relust[0] = 3;
12         relust[1] = 5;
13         relust[2] = 7;
14         int a = 0;
15         int b = 0;
16         int c = 0;
17 
18         for (int i=3;i<k ;i++ ) {
19             long temp = judge(relust[a]*3,relust[b]*5,relust[c]*7);
20             relust[i] = temp;
21             while(relust[a]*3 <= temp)a++;
22             while(relust[b]*5 <= temp)b++;
23             while(relust[c]*7 <= temp)c++;
24         }
25 
26         return relust[k-1];
27     }
28     public long judge(long a,long b,long c) {
29         long min = Math.min(a,b);
30         min = Math.min(min,c);
31         return min;
32     }
33 };

 

转载于:https://www.cnblogs.com/FJH1994/p/5041846.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值