(medium)LeetCode 264.Ugly Number II
Write a program to find the 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, 12
is the sequence of the first 10
ugly numbers.
Note that 1
is typically treated as an ugly number.
Hint:
- The naive approach is to call
isUgly
for every number until you reach the nth one. Most numbers are not ugly. Try to focus your effort on generating only the ugly ones. - An ugly number must be multiplied by either 2, 3, or 5 from a smaller ugly number.
- The key is how to maintain the order of the ugly numbers. Try a similar approach of merging from three sorted lists: L1, L2, and L3.
- Assume you have Uk, the kth ugly number. Then Uk+1 must be Min(L1 * 2, L2 * 3, L3 * 5).
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
代码如下:
public class Solution {
public int nthUglyNumber(int n) {
int [] ugly=new int[n];
ugly[0]=1;
int index2=0;
int index3=0;
int index5=0;
int factor2=2;
int factor3=3;
int factor5=5;
for(int i=1;i<n;i++){
int min=Math.min(Math.min(factor2,factor3),factor5);
ugly[i]=min;
if(factor2==min){
factor2=2*ugly[++index2];
}
if(factor3==min){
factor3=3*ugly[++index3];
}
if(factor5==min){
factor5=5*ugly[++index5];
}
}
return ugly[n-1]; }
}
运行结果:
(medium)LeetCode 264.Ugly Number II的更多相关文章
- [leetcode] 264. Ugly Number II (medium)
263. Ugly Number的子母题 题目要求输出从1开始数,第n个ugly number是什么并且输出. 一开始想着1遍历到n直接判断,超时了. class Solution { public: ...
- [LeetCode] 264. Ugly Number II 丑陋数 II
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- [LeetCode] 264. Ugly Number II 丑陋数之二
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- Leetcode 264. Ugly Number II
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- LeetCode——264. Ugly Number II
题目: Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime fact ...
- leetcode 263. Ugly Number 、264. Ugly Number II 、313. Super Ugly Number 、204. Count Primes
263. Ugly Number 注意:1.小于等于0都不属于丑数 2.while循环的判断不是num >= 0, 而是能被2 .3.5整除,即能被整除才去除这些数 class Solution ...
- 【LeetCode】264. Ugly Number II
Ugly Number II Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose ...
- 【刷题-LeetCode】264. Ugly Number II
Ugly Number II Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose ...
- 【LeetCode】264. Ugly Number II 解题报告(Java & Python)
标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ https://leetcode.com/prob ...
随机推荐
- Hadoop 源码编译导出
https://svn.apache.org/repos/asf/hadoop/common/branches/branch-trunk-win/BUILDING.txt http://www.src ...
- xshell 5连接linux服务器的技巧
1.用sttp 方式连接服务器,命令识别不了,用ssh方式才能有效
- eclipse安装完成后配置tomcat server runtime evironment
安装eclipse后创建web项目需要运行在tomcat服务器上,这里是tomcat的配置:(前提是电脑上已经安装了tomcat!)
- Httpservlet cannot be resolved to a type的原因与解决方法
刚开始学习Servlet,在Eclipse中新建了一个Servlet,不过页面上报错: Httpservlet cannot be resolved to a type,显然是Eclipse找不到相应 ...
- [java]序列化框架性能对比(kryo、hessian、java、protostuff)
序列化框架性能对比(kryo.hessian.java.protostuff) 简介: 优点 缺点 Kryo 速度快,序列化后体积小 跨语言支持较复杂 Hessian 默认支持跨语言 较慢 Pro ...
- mysql中一对一,一对多,多对多关系
打个比方吧一个班级有很多学生,他们分别有不同的学号.一个学生对应一个学号,一个学号对应一个学生:这里学生和学号的关系就是一对一.一个班级有很多学生,这个班级只有一个班主任.一个班主任对应多个学生,多个 ...
- Hibernate 抓取策略fetch-1 (select join subselect)
原文 :http://4045060.blog.51cto.com/4035060/1088025 部分参考:http://www.cnblogs.com/rongxh7/archive/2010/0 ...
- HackerRank "Manasa and Prime game"
Intuitive one to learn about Grundy basic :) Now every pile becomes a game, so we need to use Spragu ...
- winform实现word转换为PDF(.doc)
注意:实现word转换为PDF文件,本人安装Office为2013; word以后缀为.doc为例实现文件类型转换,具体方式如下所示: 实现步骤: 1.添加命名空间引用——using Microsof ...
- [系统集成] Android 自动构建系统
一.简介 android app 自动构建服务器用于自动下载app代码.自动打包.发布,要建立这样的服务器,关键要解决以下几个问题: 1. android app 自动化打包android 的打包一般 ...