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的更多相关文章

  1. [LeetCode] Ugly Number II 丑陋数之二

    Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...

  2. [LeetCode] Ugly Number 丑陋数

    Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...

  3. [LeetCode] Ugly Number II

    Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...

  4. [LeetCode] Ugly Number

    Ugly Number Total Accepted: 20760 Total Submissions: 63208 Difficulty: Easy Write a program to check ...

  5. [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 ...

  6. [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 ...

  7. LeetCode——Ugly Number

    Description: Write a program to check whether a given number is an ugly number. Ugly numbers are pos ...

  8. LeetCode() Ugly Number II 背下来!

    一个别人,非常牛逼的思路,膜拜了!orz!!!! vector <int> results (1,1); int i = 0, j = 0, k = 0; while (results.s ...

  9. LeetCode Ugly Number (简单题)

    题意: 判断是一个数的质因子仅含有2,3,5这3个. 思路: 因子2比较容易解决,num/=num-(num&num-1)就可以了.3和5的需要通过循环来另判. C++ class Solut ...

随机推荐

  1. GIL 线程池 进程池 同步 异步 阻塞 非阻塞

    1.GIL 是一个全局解释器锁,是一种互斥锁 为什么需要GIL锁:因为一个python.exe进程中只有一份解释器,如果这个进程开启了多个线程都要执行代码 多线程之间要竞争解释器,一旦竞争就有可能出现 ...

  2. 你不得不知道的5个神奇的Docker工具

    Docker社区非常活跃,每天都会推出大量有用的工具.要想持续追踪社区中发生的各项创新其实非常困难.为了帮助你,我收集了一些每天在日常工作中使用.令人感兴趣并且十分有用的Docker工具.这些工具消除 ...

  3. 在vue2.x中安装sass并配置

    在vue中安装sass先检查系统中有没有安装sass,在命令行中输入 sass -v 表示sass在电脑中已有,否者可以参考我这篇博客安装Sass遇到的坑 一.先安装sass cmd打开命令行,到项目 ...

  4. java编程--03介绍关于日期常用的计算

    /** * 获取2个日期之间的天数差 * d2-d1 * @return * @throws Exception * @Description: */ public static int getDif ...

  5. 2019.03.21 读书笔记 ==与Equals

    首先得出一个结论:==是比较变量内存的数据,Equals是值比较.但是他们都能被重写,所以object又增加了一个RefrenceEquals不可被重写,只比较数据: [ReliabilityCont ...

  6. XAMPP: Another web server is already running

    nginx 和 xampp 一起使用的时候,如果 nginx先启动,然后 再启动 xampp的时候,就是修改了 http.conf也是会报如果错误 liaohb@ubuntu-xtn->$sud ...

  7. 快速创建jquery插件

    介绍 什么是插件? 插件我们见得很多了,比如浏览器上我们会安装一些去除广告的插件.美化页面的插件等等. 在前端,我们也常常写一些jquery插件来使用.来添加我们常常写的一些功能,方便使用. 为什么要 ...

  8. opensuse13.2安装 sass和compass

    首先要先安装ruby 和 gem如果使用sudo zypper install ruby 安装后 当安装sass时会报错 /System/Library/Frameworks/Ruby.framewo ...

  9. mysql-proxy读写分离,负载均衡

    配置mysql-proxy,创建主配置文件 cd /usr/local/mysql-proxy mkdir lua #创建脚本存放目录 mkdir logs #创建日志目录 cp share/doc/ ...

  10. pat05-图2. Saving James Bond - Easy Version (25)

    05-图2. Saving James Bond - Easy Version (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作 ...