力不从心 Leetcode(ugly number heap) 263, 264,313
Leetcode ugly number set (3 now)
new ugly number is generated by multiplying a prime with previous generated ugly numbe
Is ugly number or not
class Solution {
public boolean isUgly(int num) {
if(num==0) return false;
//how to generate the number
while(num%2==0) num/=2;
while(num%3==0) num/=3;
while(num%5==0) num/=5;
return num==1;
}
}
//simulate the process to generate the ugly number
--------------------------------------------------------
next level
class Solution {
public int nthUglyNumber(int n) {
// can store the samle element
//use long to safe
PriorityQueue<Long> pq = new PriorityQueue<>();
pq.offer(1L);
int i = 2;
while(i<=n){
long smallest = pq.poll();
while(!pq.isEmpty() && smallest == pq.peek()) pq.poll();
pq.offer(smallest*2);
pq.offer(smallest*3);
pq.offer(smallest*5);
i++;
}
return pq.poll().intValue();
}
}
Key point: use the feature of PriorityQueue: take the min heap.
- PQ could have duplictae number
- Integer is not enough for this problem (negative will affect the PQ)
--------------------------------
next level there are some methods
class Solution {
public int nthSuperUglyNumber(int n, int[] primes) {
// can store the samle element
//use long to safe
PriorityQueue<Long> pq = new PriorityQueue<>();
pq.offer(1L);
int i = 2;
while(i<=n){
long smallest = pq.poll();
while(!pq.isEmpty() && smallest == pq.peek()) pq.poll();
for(int j = 0; j<primes.length; j++){
pq.offer(smallest * primes[j]);
}
i++;
}
return pq.poll().intValue();
}
}
https://leetcode.com/problems/super-ugly-number/discuss/76291/Java-three-methods-23ms-36-ms-58ms(with-heap)-performance-explained -- mark to learn
力不从心 Leetcode(ugly number heap) 263, 264,313的更多相关文章
- [LeetCode] Ugly Number II 丑陋数之二
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- [LeetCode] Ugly Number 丑陋数
Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...
- [LeetCode] Ugly Number II
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- [LeetCode] Ugly Number
Ugly Number Total Accepted: 20760 Total Submissions: 63208 Difficulty: Easy Write a program to check ...
- [LeetCode] Ugly Number II (A New Question Added Today)
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- [LeetCode] Ugly Number (A New Question Added Today)
Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...
- LeetCode——Ugly Number
Description: Write a program to check whether a given number is an ugly number. Ugly numbers are pos ...
- LeetCode() Ugly Number II 背下来!
一个别人,非常牛逼的思路,膜拜了!orz!!!! vector <int> results (1,1); int i = 0, j = 0, k = 0; while (results.s ...
- LeetCode Ugly Number (简单题)
题意: 判断是一个数的质因子仅含有2,3,5这3个. 思路: 因子2比较容易解决,num/=num-(num&num-1)就可以了.3和5的需要通过循环来另判. C++ class Solut ...
随机推荐
- hdu2795 Billboard(线段树)
题意:h*w的木板,放进一些1*L的物品,求每次放空间能容纳且最上边的位子思路:每次找到最大值的位子,然后减去L线段树功能:query:区间求最大值的位子(直接把update的操作在query里做了) ...
- powershell和cmd区别
Powershell是cmd的超集,换句话说,cmd能做的事情,Powershell都能做,但是Powershell还能额外做许多cmd不能做的活. 主要是系统管理功能.脚本语言和在线帮助更强大,你确 ...
- day15 面向对象 成员
成员 1. 变量 1.实例变量 格式: 变量.xxx=xx (称为实例变量,也叫属性,字段)给对象用的 2.类变量 类变量:直接写在类中的变量就是类变量,类变量一般用类名来访问 其实就是类中相同的属性 ...
- NFS网络储存系统
为什么用NFS网络文件存储系统? 1)实现数据信息统一一致 2)节省局域网数据同步传输的带宽 3)节省网站架构中服务器硬盘资源 NFS系统存储原理介绍 RPC服务类似一个中介服务,NFS服务端与NFS ...
- daterangepicker-双日历
js脚本和css样式,到bootstrap官网去下载 <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml&qu ...
- Why do you need a new Launch X431 scan tool?
1- 2017 Launch x431 v supports “Special Functions” The 2017 version of Launch x431 v diagnostic tool ...
- CSS background-size contain 与cover的区别
最近在重温CSS,发现好多东西都忘了,比如background-size属性中,contain与cover的区别. 菜鸟教程上是这么说的: 有点难理解,通俗解释就是:两者均以保持图像宽高比的形式缩放来 ...
- python 编程,应该养成哪些好的习惯
python 编程,应该养成哪些好的习惯? https://www.zhihu.com/question/28966220 无缓冲输出 python -u sys.stdout.flush() 性能 ...
- 用Java 8 Lambda表达式实现设计模式:命令模式
在这篇博客里,我将说明如何在使用 Java 8 Lambda表达式 的函数式编程方式 时实现 命令 设计模式 .命令模式的目标是将请求封装成一个对象,从对客户端的不同类型请求,例如队列或日志请求参数化 ...
- jQuery综述
jQuery是一个轻量级JavaScript库 它可以进行如下操作: 选取HTML元素 对HTML元素进行操作 对CSS进行操作 编写HTML事件函数 JavaScript特效 HTML DOM AJ ...