剑指Offer-34
来源:互联网 发布:js设置html font size 编辑:程序博客网 时间:2024/06/06 14:48
题目:
我们把只包含因子 2,3,5的数称为丑数,求按从小到大的顺序的第1500个丑数。(习惯上把 1 当作第一个丑数)
实现
// coding = javapublic class Solution34 { public static int getUglyNum(int k){ if(k<1){ return 0; } int[] ugly_nums = new int[k]; ugly_nums[0]=1; int index = 1; int p2 = 0; int p3 = 0; int p5 = 0; while(index<k){ int min = min_num(ugly_nums[p2]*2,ugly_nums[p3]*3,ugly_nums[p5]*5); ugly_nums[index] = min; while (ugly_nums[p2]*2<=ugly_nums[index]){ p2++; } while (ugly_nums[p3]*3<=ugly_nums[index]){ p3++; } while (ugly_nums[p5]*5<=ugly_nums[index]){ p5++; } index++; } return ugly_nums[index-1]; } public static int min_num(int num1,int num2,int num3){ int min = num1<num2?num1:num2; min = min<num3?min:num3; return min; } public int getUglyNum2(int k){ if(k<1) { System.out.print("输入不合法"); return -1; } int current_num = 0; int count=0; while (count<k){ current_num++; if(isUgly(current_num)){ count++; } } return current_num; } public boolean isUgly(int num){ while (num%2==0){ num/=2; } while (num%3==0){ num/=3; } while (num%5==0){ num/=5; } return num==1?true:false; } public static void main(String[] args) { int result = getUglyNum(1500); System.out.print(result); }}
阅读全文
1 0
- 剑指Offer-34
- 剑指offer面试题34
- 剑指offer 34- 丑数
- 剑指offer 34---丑数
- 剑指offer 34 丑数
- 剑指OFFER
- 剑指offer
- 剑指Offer
- 剑指offer
- 剑指Offer
- 剑指offer
- 剑指offer
- 剑指offer
- 剑指Offer
- 剑指offer
- 剑指offer
- 剑指Offer
- 剑指offer
- RecyclerView的用法和ButterKnife的配置
- 4.4 双线性变换法
- Ubuntu14.04下安装opencv3(anaconda3 Python3.6)
- 网络编程
- Android多线程之AsyncTask
- 剑指Offer-34
- tiny4412,将u-boot烧录到eMMC中并启动
- Usage of Inner class in java,java 内部类的使用
- W: GPG 错误:http://ppa.launchpad.net lucid Release: 由于没有公钥,无法验证下列签名: NO_PUBKEY FAF69C646FF368B7的问题
- Linux crontab 命令详解
- iOS JSPatch 热修复实现原理详解
- ESP8266新手入门------百家长,附带STM32程序等
- 【GPU】基于VS2012 的NVIDA OpenCL安装与配置
- 4.5 matlab 设计IIR滤波器