标签(空格分隔): LeetCode

作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/

https://leetcode.com/problems/ugly-number-ii/

Total Accepted: 12227 Total Submissions: 54870 Difficulty: Medium

Question

Write a program to find the n-th ugly number.

Ugly numbers are positive numbers whose prime factors only include 2, 3, 5.

Examples

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.

Ways

方法一

最简单的方法就是遍历的方法,直接判断每个数是不是丑数,如果是就加入list中,这就是方法一。

但是这个方法效率太低,提交的时候连续三次都超出时间限制了。本地运行还是可以的。迫于无奈,必须提高效率。

方法二

所有的 ugly number 都是由 1 开始,乘以 2/3/5 生成的。

只要将这些生成的数排序即可获得,自动排序可以使用 set

这样每次取出的第一个元素就是最小元素,由此再继续生成新的ugly number.

可以分成如下三组:

(1) 1×2, 2×2, 3×2, 4×2, 5×2, …

(2) 1×3, 2×3, 3×3, 4×3, 5×3, …

(3) 1×5, 2×5, 3×5, 4×5, 5×5, …

使每个组已经用过的数字删除掉,这样列表中只有一个元素,获取三个组的最小值之后就计算下一个丑数。

public static int nthUglyNumber2(int n) {
List<Integer> num2List = new ArrayList<Integer>();
List<Integer> num3List = new ArrayList<Integer>();
List<Integer> num5List = new ArrayList<Integer>(); num2List.add(1);
num3List.add(1);
num5List.add(1); int test = 0; for (int j = 0; j < n; j++) {
//最小元素
test = Math.min(Math.min(num2List.get(0), num3List.get(0)), num5List.get(0)); //让列表中一直只有一个元素
if (num2List.get(0) == test) num2List.remove(0);
if (num3List.get(0) == test) num3List.remove(0);
if (num5List.get(0) == test) num5List.remove(0); num2List.add(2 * test);
num3List.add(3 * test);
num5List.add(5 * test);
} return test;
}

二刷,python

class Solution(object):
def nthUglyNumber(self, n):
if n < 0:
return 0
dp = [1] * n
index2, index3, index5 = 0, 0, 0
for i in range(1, n):
dp[i] = min(2 * dp[index2], 3 * dp[index3], 5 * dp[index5])
if dp[i] == 2 * dp[index2]: index2 += 1
if dp[i] == 3 * dp[index3]: index3 += 1
if dp[i] == 5 * dp[index5]: index5 += 1
return dp[n - 1]

Reference

http://blog.csdn.net/guang09080908/article/details/47780619

http://blog.csdn.net/xudli/article/details/47903959

Date

2015/10/18 20:57:01

2018 年 8 月 28 日 ———— 雾霾天

【LeetCode】264. Ugly Number II 解题报告(Java & Python)的更多相关文章

  1. 【LeetCode】137. Single Number II 解题报告(Python)

    [LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...

  2. [LeetCode] 264. Ugly Number II 丑陋数 II

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

  3. [leetcode] 264. Ugly Number II (medium)

    263. Ugly Number的子母题 题目要求输出从1开始数,第n个ugly number是什么并且输出. 一开始想着1遍历到n直接判断,超时了. class Solution { public: ...

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

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

  5. Leetcode 264. Ugly Number II

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

  6. (medium)LeetCode 264.Ugly Number II

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

  7. LeetCode——264. Ugly Number II

    题目: Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime fact ...

  8. 【LeetCode】575. Distribute Candies 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

  9. 【LeetCode】383. Ransom Note 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...

随机推荐

  1. shell编程100列

    1.编写hello world脚本 #!/bin/bash# 编写hello world脚本 echo "Hello World!"2.通过位置变量创建 Linux 系统账户及密码 ...

  2. shell 脚本的基本定义

    注意不能有控制,指令之间 [1]shell脚本的基础知识 (1)shell脚本的本质 编译型语言 解释型语言 shell脚本语言是解释型语言 shell脚本的本质 shell命令的有序集合 (2)sh ...

  3. 详解 Rainbond Ingress 泛解析域名机制

    Rainbond 作为一款云原生应用管理平台,天生带有引导南北向网络流量的分布式网关 rbd-gateway.区别于一般的 Ingress 配置中,用户需要自行定义域名的使用体验,Rainbond 的 ...

  4. 【Penetration】红日靶场(一)

    nmap探查存活主机 nmap -sP 10.10.2.0/24 图片: https://uploader.shimo.im/f/cfuQ653BEvyA42FR.png!thumbnail?acce ...

  5. 案例 stm32单片机,adc的双通道+dma 内部温度

    可以这样理解 先配置adc :有几个通道就配置几个通道. 然后配置dma,dma是针对adc的,而不是针对通道的. 一开始我以为一个adc通道对应一个dma通道.(这里是错的,其实是我想复杂了) 一个 ...

  6. Output of C++ Program | Set 1

    Predict the output of below C++ programs. Question 1 1 // Assume that integers take 4 bytes. 2 #incl ...

  7. Linux学习 - 网络命令

    一.write 1 功能 给指定在线用户发信息,以Ctrl + D保存结束 2 语法 write  <用户名>  [信息] 二.wall(write all) 1 功能 给所有在线用户发送 ...

  8. my37_MGR流控对数据库性能的影响以及MGR与主从的性能对比

    mysql> show variables like 'group_replication_flow_control_applier_threshold'; +----------------- ...

  9. 1.RabbitMQ

    1.RabbitMq是什么?    MQ全称为Message Queue,即消息队列, RabbitMQ是由erlang语言开发,基于AMQP(Advanced Message Queue 高级消息队 ...

  10. 1.Vuejs-第一个实例

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...