剑指Offer系列-面试题34:丑数
来源:互联网 发布:太极拳威力知乎 编辑:程序博客网 时间:2024/06/12 00:09
题目:把只包含因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含因子7。习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第N个丑数。
思路:
代码:
public int GetUglyNumber_Solution(int index) {if (index <= 0) {return 0;} else if (index < 7) {return index;}int[] nums = new int[index];nums[0] = 1;int t2 = 0;int t3 = 0;int t5 = 0;for ( int i = 1 ; i < index ; i++ ) { // 每次计算出下一个丑数是多少并放入数组中nums[i] = Math.min(nums[t2] * 2, Math.min(nums[t3] * 3, nums[t5] * 5));if (nums[i] == nums[t2] * 2) {t2++;}if (nums[i] == nums[t3] * 3) {t3++;}if (nums[i] == nums[t5] * 5) {t5++;}}return nums[index - 1];}
0 0
- 剑指Offer系列-面试题34:丑数
- [剑指offer][面试题34]丑数
- [剑指offer]面试题34:丑数
- 剑指Offer:面试题34 丑数
- 剑指offer 面试题34 丑数
- 剑指offer-面试题34:丑数
- 剑指offer面试题34:丑数
- 剑指Offer----面试题34:丑数
- 剑指offer面试题34:丑数
- 剑指offer--面试题34:丑数
- 【剑指offer】面试题34-丑数
- 剑指offer-面试题34-丑数
- 剑指offer-面试题34-丑数
- 剑指offer 面试题34 丑数
- 剑指offer面试题[34]丑数
- 【剑指Offer】面试题34:丑数
- 《剑指Offer》面试题:丑数
- 【剑指offer】面试题49:丑数
- 获取mac地址和IP地址方式
- 拦截器
- 什么是WEB
- Spring websocket ssl和摘要认证
- HDU1016
- 剑指Offer系列-面试题34:丑数
- One python demo to analysis the decoding time and update texture and swapbuffer time
- 负载均衡的四层和七层区别
- 3、编辑器--动作编辑器需求
- 国际化
- 同一进程下的线程可以共享哪些资源
- 史上最全的Maven Pom文件标签详解
- 哈密顿图
- 动态方法访问