剑指offer: 丑数
来源:互联网 发布:centos更新python 编辑:程序博客网 时间:2024/04/30 04:53
题目:
把只包含因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含因子7。 习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第N个丑数。
思路:
1.下一个丑数必定由有数组中的某一个丑数A * 2, B * 3, C * 5 的中的最小值得来。
2.用数组保存丑数
代码:
class Solution{ public int GetUglyNumber_Solution(int index) { // write code here if(index <= 0) return 0; int[] ugly = new int[index]; ugly[0] = 1; int index2 = 0; int index3 = 0; int index5 = 0; for(int i=1; i<index; i++) { ugly[i] = Min(ugly[index2]*2,ugly[index3]*3,ugly[index5]*5); if(ugly[index2]*2 <= ugly[i]) index2++; if(ugly[index3]*3 <= ugly[i]) index3++; if(ugly[index5]*5 <= ugly[i]) index5++; } return ugly[index - 1]; } public int Min(int a, int b, int c) { return (a<b?a:b)<c?(a<b?a:b):c; }}
0 0
- 【剑指offer】丑数
- 剑指offer-丑数
- 《剑指offer》丑数
- 【剑指Offer】丑数
- 剑指offer:丑数
- 剑指offer-丑数
- [剑指offer]丑数
- 剑指offer:丑数
- [剑指offer]丑数
- 《剑指offer》-丑数
- 剑指offer----丑数
- 剑指offer|丑数
- 剑指offer-丑数
- 剑指Offer:丑数
- 剑指offer--丑数
- 剑指offer-丑数
- 剑指offer 丑数
- 剑指offer-丑数
- 关系运算符——三个数排序
- 一般图最大匹配 带花树算法 模板
- IntelliJ IDEA 创建 hello world Java web Maven项目从头到尾都有图有真相2017版本
- 题目1123:采药
- 安装phpredis插件出现的问题
- 剑指offer: 丑数
- 云平台仿真框架CloudSim
- 0319
- Java关键字 super和this
- V4L2采集摄像头过程中的几点细节
- mysql sql order
- 为什么要重写hashCode()方法和equals()方法以及如何进行重写
- ACM程序设计 书中题目U(美丽的数字)
- 走进 Redis 的世界