Ugly Number II 解答
Question
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).
Solution -- Queue
根据hint,我们维护三个队列。分别用来存当前的ugly number与2,3,5相乘后的结果。
选三个队列的队头元素最小的那个即是下一个ugly number。
public class Solution {
public int nthUglyNumber(int n) {
if (n < 2) {
return 1;
}
Queue<Integer> twoNumbers = new LinkedList<Integer>();
Queue<Integer> threeNumbers = new LinkedList<Integer>();
Queue<Integer> fiveNumbers = new LinkedList<Integer>();
twoNumbers.offer(2);
threeNumbers.offer(3);
fiveNumbers.offer(5);
int result = 1;
while (n > 1) {
int candidate1 = twoNumbers.peek(), candidate2 = threeNumbers.peek(), candidate3 = fiveNumbers.peek();
// find minimum number
result = Math.min(candidate1, candidate2);
result = Math.min(candidate3, result);
// add new numbers
twoNumbers.offer(result * 2);
threeNumbers.offer(result * 3);
fiveNumbers.offer(result * 5);
// remove existing minimums
if (result == candidate1) {
twoNumbers.poll();
}
if (result == candidate2) {
threeNumbers.poll();
}
if (result == candidate3) {
fiveNumbers.poll();
}
n--;
}
return result;
}
}
Ugly Number II 解答的更多相关文章
- 【LeetCode】264. Ugly Number II
Ugly Number II Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose ...
- Ugly Number,Ugly Number II,Super Ugly Number
一.Ugly Number Write a program to check whether a given number is an ugly number. Ugly numbers are po ...
- 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 (medium)
263. Ugly Number的子母题 题目要求输出从1开始数,第n个ugly number是什么并且输出. 一开始想着1遍历到n直接判断,超时了. class Solution { public: ...
- Leetcode之动态规划(DP)专题-264. 丑数 II(Ugly Number II)
Leetcode之动态规划(DP)专题-264. 丑数 II(Ugly Number II) 编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n ...
- 【刷题-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@ [263/264] Ugly Numbers & Ugly Number II
https://leetcode.com/problems/ugly-number/ Write a program to check whether a given number is an ugl ...
- [LeetCode] 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 ...
随机推荐
- [原创作品] web项目构建(一)
今天开始,将推出web项目构建教程,与<javascript精髓整理篇>一并更新.敬请关注. 这篇作为这一系列开头,主要讲述web项目的构建技术大全.在众多人看来,web前端开发无非就是写 ...
- Mac phpstorm破解版安装(简单,有效)
如果是公司作为商业用途的,还是希望你能购买正版的,如果是苦逼的穷学生,亦或是我这样的苦逼码农,那就往下看, 之前有个只需要在"License server address"里输入 ...
- 只要关闭浏览器,session就消失了
程序一般都是在用户做log off的时候发个指令去删除session,然而浏览器从来不会主动在关闭之前通知服务器它将要被关闭,因此服务器根本不会有机会知道浏览器已经关闭.服务器会一直保留这个会话对象直 ...
- [React Testing] Intro to Shallow Rendering
In this lesson, we walk through how to use one of React's Test Utilities (from thereact-addons-test- ...
- ASP.NET获取用户端的真实IP
ASP.NET获取用户端的真实IP在各种场景都能用到,但是用户网端变幻莫测情况众多,获取真实IP还真是不容易啊.下面分享个比较好一点的方法: 获取IP初始版本 /// <summary> ...
- 前端 HTML基础
前端三大利器概述 学习前端,不得不学习前端中的三大利器:html + css + javascript.那么这三个组件分别起到什么作用呢?以人体为例,单单具有html属性的人,只是一个裸体的人偶(理解 ...
- 手贱随手在Linux敲了 as 命令,出不来了
手贱随手在Linux敲了 as 命令,出不了命令,问问度娘吧,得到下列资料 as命令 GNU组织推出的一款汇编语言编译器,它支持多种不同类型的处理器.语法as(选项)(参数)选项-ac:忽略失败条 ...
- Python代码分析工具之dis模块
转自:http://hi.baidu.com/tinyweb/item/923d012e8146d00872863ec0 ,格式调整过. 代码分析不是一个新的话题,代码分析重要性的判断比较主观,不同 ...
- include子页面传递过来的参数传递到后台
在页面上可以使用 ${param.moduleId}来获取 在判断中也可以使用${param.moduleId == "test" ? "1":"2& ...
- Red Hat Enterprise Linux x86-64 上安装 oracle 11gR2
一.以root用户登录 二.安装依赖包 #rpm -qa | grep 包名 ----查看包 binutils-2.20.51.0.2-5.11.el6 (x86_64) ...