力不从心 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 ...
随机推荐
- 75th LeetCode Weekly Contest All Paths From Source to Target
Given a directed, acyclic graph of N nodes. Find all possible paths from node 0 to node N-1, and re ...
- GC:并行回收CMS详解
CMS详解 https://www.cnblogs.com/ggjucheng/p/3977612.html CMS默认不回收Perm, 需要加参数 +CMSPermGenSweepingEnable ...
- 使用xUnit为.net core程序进行单元测试
第1部分: http://www.cnblogs.com/cgzl/p/8283610.html 第2部分: http://www.cnblogs.com/cgzl/p/8287588.html ...
- my02_Atlas mysql5.7安装配置
软件环境:centos7.3,glib-2.49,lua5.1,Atlas2.2.1,mysql5.7 依赖包安装******************************************* ...
- TypeScript -- JavaScript的救赎
TypeScript的设计目的应该是解决JavaScript的"痛点":弱类型和没有命名空间,导致很难模块化,不适合开发大型程序.另外它还提供了一些语法糖来帮助大家更方便地实践面向 ...
- Murano py27和py34的兼容处理
tox.ini envlist = py27,py34,pep8 1. django.utils.encoding.force_unicode替换成django.utils.encoding.forc ...
- 020-pom.xml配置文件模板
1 Maven 整合SSH框架之pom.xml 1 版本一 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns: ...
- ssh设置别名
通常我们在 Termianl 下用 ssh 链接远程主机的时候,每次都需要输入一长串的用户名加主机地址,是不是觉得很麻烦? 我们知道在 /etc/ssh/ 目录下通常都会有 ssh_config 和 ...
- asp实现网页浏览总数
<% AlldayView=0 Set Rs=Server.CreateObject("Adodb.RecordSet") Sql="select * from v ...
- DataGridView进度条列 C# WinForm
先看看效果,如果感兴趣,继续往下看…… 效果如下图所示: DataGridView里没有Pragress列,但有Image列,有了它我们可以自己绘图来实现进度条.其实实现起来并不困难. 首先在实体类增 ...