丑数(LintCode)
2024-09-05 13:25:36
丑数
设计一个算法,找出只含素因子3
,5
,7
的第 k大的数。
符合条件的数如:3,5,7,9,15......
您在真实的面试中是否遇到过这个题?
Yes
样例
如果k=4
, 返回 9
挑战
要求时间复杂度为O(nlogn)或者O(n)
import java.util.Queue;
import java.util.LinkedList; class Solution {
/**
* @param k: The number k.
* @return: The kth prime number as description.
*/
public long kthPrimeNumber(int k) {
long[] relust = new long[k];
relust[0] = 3;
relust[1] = 5;
relust[2] = 7;
int a = 0;
int b = 0;
int c = 0; for (int i=3;i<k ;i++ ) {
long temp = judge(relust[a]*3,relust[b]*5,relust[c]*7);
relust[i] = temp;
while(relust[a]*3 <= temp)a++;
while(relust[b]*5 <= temp)b++;
while(relust[c]*7 <= temp)c++;
} return relust[k-1];
}
public long judge(long a,long b,long c) {
long min = Math.min(a,b);
min = Math.min(min,c);
return min;
}
};
最新文章
- PHP基础知识之常量
- Windows简单几步实现系统自动关机设置
- Linux C 文件输入输出函数 fopen()、getc()/fgetc()、putc()/fputc()、fclose()、fprintf()、fscanf()、fgets()、fputs()、fseek()、ftell()、fgetpos()、fsetpos() 详解
- Android应用开发-数据存储和界面展现(一)(重制版)
- Python的第四天
- 远程桌面不能连接,提示awgina.dll取代错误的解决办法
- a与a:link、a:visited、a:hover、a:active
- 【POJ】2318 TOYS(计算几何基础+暴力)
- 根据headerView位置改变headerView颜色(collectionView/tableview)
- OpenCV学习 2:播放AVI视频
- Python:2D画图库matplotlib学习总结
- mysql 千万量级的表的优化
- 【算法系列学习】Dijkstra求最短路 [kuangbin带你飞]专题四 最短路练习 D - Silver Cow Party
- linux设备驱动之字符设备驱动模型(2)
- select * 和 select 所有字段的区别
- python 11
- springboot调优
- centos 7.5安装docker-CE 18
- 2018下半年Android面试历程
- JS笔记—01