力不从心 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 ...
随机推荐
- 你不得不知道的5个神奇的Docker工具
Docker社区非常活跃,每天都会推出大量有用的工具.要想持续追踪社区中发生的各项创新其实非常困难.为了帮助你,我收集了一些每天在日常工作中使用.令人感兴趣并且十分有用的Docker工具.这些工具消除 ...
- Photoshop入门教程(一):文本新建与概念解析
写在开头 <Photoshop实用入门>系列教程可能对于一点都没有接触过Photoshop的人来说不太容易接受,因为本教程并没有细致到教你如何使用画笔工具等一系列很基础的东西,有些地方的讲 ...
- /etc目录下重要的目录和文件
牢记: 1.*/etc/sysconfig/network-scripts/ifcfg-eth0 网卡的配置信息 重启网卡:1)/etc/init.d/network restart(所有网卡) ...
- vim的三种模式
vim的三种模式(最基本的) 命令模式:在该模式下是不能对文件进行编辑的,可以输入快捷键进行一些操作(删除. 复制.移动光标.粘贴)[打开默认 是进入命令模式] 编辑 ...
- java——io、字节流缓冲区拷贝文件、字节缓冲流
使用try catch finally关闭文件流: 写入文件: import java.io.*; public class exp{ public static void main(String[] ...
- Microsoft使用技巧
1.拍摄屏幕内容的截图 按 Win + Shift + S 以打开截图栏,然后将光标拖动到要捕获的区域. 截图区域将保存到剪贴板. 2.使用键盘添加表情符号 随心随处表达自我. 按 Ctrl + Sh ...
- VIM操作手札
查看帮助手册 [Vim 中文帮助文档] 常用命令及说明 在命令模式下编辑 命令 说明 Ctrl+u 向文件首翻半屏 Ctrl+d 向文件尾翻半屏 Ctrl+f 向文件尾翻一屏 Ctrl+b 向文件首翻 ...
- python自学-day2(变量、if条件判断、运算符操作)
1.变量 变量只是用于保存内存位置,将变量存储在内存中的作用,方便后面调用,这意味着,在创建变量时会在内存中开辟一个空间. 变量命名规则: 由字母.数字.下划线(_)组成 不能以数字开头 不能使用 P ...
- ORACLE分页SQL
1,使用rownum SELECT * FROM ( SELECT A.*, ROWNUM RN FROM (SELECT * FROM TABLE_NAME) A ) 2,使用between SEL ...
- 【密码学】CSP的概念
CSP加密服务提供者(Cryptographic Service Provider)具有一下几个特点: CSP是真正执行密码运算的独立模块 物理上一个CSP由两部分组成:一个动态连接库,一个签名文件 ...