[Leetcode] Ugly Number II
2015-10-14 11:18
447 查看
Write a program to find the
Ugly numbers are positive numbers whose prime factors only include
Note that
public class Solution {
public int nthUglyNumber(int n) {
List<Integer> record = new ArrayList<>();
record.add(1);
int twoPosition = 0;
int threePosition = 0;
int fivePosition = 0;
while(record.size() < n){
int nextUglyNumber = Math.min(record.get(twoPosition)*2, Math.min(record.get(threePosition)*3, record.get(fivePosition)*5));
record.add(nextUglyNumber);
int lastElement = nextUglyNumber;
if( lastElement == record.get(twoPosition)*2)
twoPosition++;
if(lastElement == record.get(threePosition) * 3)
threePosition++;
if(lastElement == record.get(fivePosition) * 5)
fivePosition++;
}
return record.get(record.size()-1);
}
}
n-th ugly number.
Ugly numbers are positive numbers whose prime factors only include
2, 3, 5. For example,
1, 2, 3, 4, 5, 6, 8, 9, 10, 12is the sequence of the first
10ugly numbers.
Note that
1is typically treated as an ugly number.
public class Solution {
public int nthUglyNumber(int n) {
List<Integer> record = new ArrayList<>();
record.add(1);
int twoPosition = 0;
int threePosition = 0;
int fivePosition = 0;
while(record.size() < n){
int nextUglyNumber = Math.min(record.get(twoPosition)*2, Math.min(record.get(threePosition)*3, record.get(fivePosition)*5));
record.add(nextUglyNumber);
int lastElement = nextUglyNumber;
if( lastElement == record.get(twoPosition)*2)
twoPosition++;
if(lastElement == record.get(threePosition) * 3)
threePosition++;
if(lastElement == record.get(fivePosition) * 5)
fivePosition++;
}
return record.get(record.size()-1);
}
}
相关文章推荐
- win7、win8.1怎么更换系统壁纸?win8.1更换系统壁纸操作
- mic的灵敏度和声音的大小
- dspmq dspmqver command not found(dspmq命令找不到,dspmqver主安装目录设置不正确)
- SqlServer事务回滚
- 用CSS绘制梯形
- lucene之得到当前线程对应的文档集处理对象DocumentsWriterThreadState
- [转]Linq语法二
- 捕android程序崩溃日志
- C#创建不规则窗体的4种方式详解
- swift的简单了解
- 单链表实现(student);
- 安装sipp测试工具
- UITableView详谈
- ETC1压缩纹理格式详解
- 练习:输出整数每一位,计算算数,9出现次数,输出图案,水仙花数
- accelio介绍和信息汇总
- 中小手机厂商遇倒闭潮:沦为小米华为炮灰
- JavaScript属性
- Android:下载网络图片
- nodejs API 之典型概念