(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 ...
随机推荐
- MVC2、MVC3、MVC4、MVC5之间的区别 以及Entity Framework 6 Code First using MVC 5官方介绍教程
现在MVC的技术日趋成熟,面对着不同版本的MVC大家不免有所迷惑 -- 它们之间有什么不同呢?下面我把我搜集的信息汇总一下,以便大家能更好的认识不同版本MVC的功能,也便于自己查阅. View Eng ...
- python数据类型之int用法
1.查看整型的用法 CODE:print(dir(int))['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', ...
- 简述oracle视图
1.视图的概述 视图其实就是一条查询sql语句,用于显示一个或多个表或其他视图中的相关数据.视图将一个查询的结果作为一个表来使用,因此视图可以被看作是存储的查询或一个虚拟表.视图来源于表,所有对视图数 ...
- 正在调用的 ServicedComponent 配置不正确(请使用 regsvcs 重新注册)
问题: 正在调用的 ServicedComponent 配置不正确(请使用 regsvcs 重新注册) 方法1: 我用的是64位操作系统.IIS中,启用32位应用程序设置为false.这样就可以了 ...
- CryptoAPI与openssl RSA非对称加密解密(PKCS1 PADDING)交互
(以下代码中都只做测试用,有些地方没有释放内存...这个自己解决下) 1.RSA非对称的,首先提供一个供测试用的证书和私钥的数据 1)pem格式的证书和私钥(公私钥是对应的)的base64编码 voi ...
- 使用 Windows PowerShell 来管理和开发 windowsazure.cn 账户的特别注意事项
6月6日,微软面向中国大陆用户开放了Microsoft Azure公众预览版的申请界面.大家可以申请免费的 beta 试用,收到内附邀请码的通知邮件后只需输入激活码即可开始免费试用.具体网址为: ht ...
- 剑指offer系列24---数组中重复的数字
* [24] * [题目]在一个长度为n的数组里的所有数字都在0到n-1的范围内. * 数组中某些数字是重复的,但不知道有几个数字是重复的. * 也不知道每个数字重复几次. * 请找出数组中任意一个重 ...
- 【KVM】Ubuntu14.04 安装KVM
1. 首先检查系统是否支持CPU虚拟化 # egrep -o "svm|vmx" /proc/cpuinfo 若显示如下类似信息,则说明支持CPU虚拟化 vmx vmx ... v ...
- Xshell远程连接Linux时无法使用小键盘的解决方式
我在用xshell连接远程的centos时,每次使用vi/vim的时候而NumLock明明在开启着,小键盘都不能正确输入数字,其实这是时按小而是出现一个字母然后换行(实际上是命令模式上对应上下左右的键 ...
- Add 1G to a LVM on VMware
1. update disk1 to 5G from 4G in vcenter2. echo 1 > /sys/block/sda/device/rescan3. fdisk /dev/sda ...