[Leetcode] 丑数问题

Ugly Number

Write a program to check whether a given number is an ugly number.

Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7.

Note that 1 is typically treated as an ugly number.

Ugly Number II

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.

又是一个找第K小的问题,老题重谈了。

一般思路都是从当前最小,找出后面的数字,并且放到优先队列当中去进行保存。

一、ugly number

 1 public class Solution {
 2     public boolean isUgly(int num) {
 3         if(num<=0) return false;
 4         if(num==1) return true;
 5         while(num!=1){
 6             if(num%2==0) num/=2;
 7             else if(num%3==0) num/=3;
 8             else if(num%5==0) num/=5;
 9             else return false;
10         }
11         return true;
12     }
13 }

二、ugly number II

 1 import java.util.*;
 2
 3 public class Solution {
 4     public int nthUglyNumber(int n) {
 5         if(n==1) return 1;
 6         //这里需要使用Double来保存数据,使用int会溢出
 7         PriorityQueue<Double> q2 = new PriorityQueue<Double>();
 8         PriorityQueue<Double> q3 = new PriorityQueue<Double>();
 9         PriorityQueue<Double> q5 = new PriorityQueue<Double>();
10         q2.offer(1.0);
11         double min =1;
12         for(int i=1;i<=n;i++){
13             min = q2.peek().intValue();
14             min = !q3.isEmpty()&&q3.peek().intValue()<min? q3.peek().intValue():min;
15             min = !q5.isEmpty()&&q5.peek().intValue()<min? q5.peek().intValue():min;
16             if(min==q2.peek().intValue()){
17                 q2.poll();
18                 q2.offer(2*min);
19                 q3.offer(3*min);
20                 q5.offer(5*min);
21             }else if(min==q3.peek().intValue()){
22                 q3.poll();
23                 q3.offer(3*min);
24                 q5.offer(5*min);
25             }else{
26                 q5.poll();
27                 q5.offer(min*5);
28             }
29         }
30         return (int)min;
31     }
32 }
时间: 2024-08-01 00:05:02

[Leetcode] 丑数问题的相关文章

leetcode 丑数 II java

题目: 编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n = 10输出: 12解释: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 是前 10 个丑数.说明: 1 是丑数.n 不超过1690. 解题: 使用三指针 class Solution { public int nthUglyNumber(int n) { int min2 = 0; int min3 = 0; int min5 = 0; int []result =

LeetCode——264. 丑数 II

编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n = 10 输出: 12 解释: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 是前 10 个丑数. 说明: 1 是丑数. n 不超过1690. 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/ugly-number-ii 1.暴力(brute force) class Solution { public: int nth

LeetCode OJ:Ugly Number(丑数)

Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7. Note that 1 is ty

[LeetCode]70. Ugly Number II第N个丑数

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

【leetcode 简单】 第七十三题 丑数

编写一个程序判断给定的数是否为丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例 1: 输入: 6 输出: true 解释: 6 = 2 × 3 示例 2: 输入: 8 输出: true 解释: 8 = 2 × 2 × 2 示例 3: 输入: 14 输出: false 解释: 14 不是丑数,因为它包含了另外一个质因数 7. 说明: 1 是丑数. class Solution: def isUgly(self, num): """ :type num: int :

Leetcode 264.丑数II

丑数II 编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n = 10 输出: 12 解释: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 是前 10 个丑数. 说明:  1 是丑数. n 不超过1690. 思路:动态规划思想.后面的丑数一定是由前面的丑数乘以2.3或5得到.所以第n个丑数一定是由前n-1个数中的某3个丑数(分别记为index2.index3.index5)分别乘以2.3或者5得到的数中的最小数,index2,

Leetcode之动态规划(DP)专题-264. 丑数 II(Ugly Number II)

编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n = 10 输出: 12 解释: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 是前 10 个丑数. 说明: 1 是丑数. n 不超过1690. dp含义: dp[i]表示第i-1个丑数. class Solution { public int nthUglyNumber(int n) { int[] dp = new int[n + 1]; dp[0] = 1; int i2

[剑指offer] 33. 丑数

题目描述 把只包含质因子2.3和5的数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因为它包含质因子7. 习惯上我们把1当做是第一个丑数.求按从小到大的顺序的第N个丑数.  leetcode原题,题解链接 class Solution { public: int GetUglyNumber_Solution(int index) { if (index < 7) return index; vector<int> res(index); res[0] = 1; in

Humble Numbers(丑数) 超详解!

给定一个素数集合 S = { p[1],p[2],...,p[k] },大于 1 且素因子都属于 S 的数我们成为丑数(Humble Numbers or Ugly Numbers),记第 n 大的丑数为 h[n]. 算法 1: 一种最容易想到的方法当然就是从 2 开始一个一个的判断一个数是否为丑数.这种方法的复杂度约为 O( k * h[n]),铁定超时(如果你这样做而没有超时,请跟 tenshi 联系) 算法 2: 看来只有一个一个地主动生成丑数了 : 我最早做这题的时候,用的是一种比较烂的