【LeetCode】264. Ugly Number II 解题报告(Java & Python)
标签(空格分隔): 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)的更多相关文章
- 【LeetCode】137. Single Number II 解题报告(Python)
[LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...
- [LeetCode] 264. Ugly Number II 丑陋数 II
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- [leetcode] 264. Ugly Number II (medium)
263. Ugly Number的子母题 题目要求输出从1开始数,第n个ugly number是什么并且输出. 一开始想着1遍历到n直接判断,超时了. class Solution { public: ...
- [LeetCode] 264. Ugly Number II 丑陋数之二
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- Leetcode 264. Ugly Number II
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- (medium)LeetCode 264.Ugly Number II
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- LeetCode——264. Ugly Number II
题目: Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime fact ...
- 【LeetCode】575. Distribute Candies 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- 【LeetCode】383. Ransom Note 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...
随机推荐
- Linux生信服务器磁盘如何挂载使用?
用过很多服务器,但一直没自己挂载过磁盘,因为待挂载的磁盘上都有数据,生怕一不小心把别人的弄坏了. 今天恰好有几块新的磁盘,供我尝试下. 首先查看下磁盘: $ df -h 文件系统 容量 已用 可用 已 ...
- python-django-常用models里面的Field
1.models.AutoField 自增列 = int(11) 如果没有的话,默认会生成一个名称为 id 的列 如果要显式的自定义一个自增列,必须设置primary_key=True. 2.mode ...
- 关于写SpringBoot+Mybatisplus+Shiro项目的经验分享四:部署到阿里云
框架: SpringBoot+Mybatisplus+Shiro 简单介绍:关于写SpringBoot+Mybatisplus+Shiro项目的经验分享一:简单介绍 阿里云开放必要端口,mysql与t ...
- 『与善仁』Appium基础 — 18、元素定位工具(二)
目录 1.Appium Inspector介绍 2.Appium Inspector打开方式 3.Appium Inspector布局介绍 4.Appium Inspector工具的配置 5.Appi ...
- 03 Windows安装Java环境
Java环境安装 使用微信扫码关注微信公众号,并回复:"Java环境",免费获取下载链接! 1.卸载(电脑未装此程序,跳过此过程) 找到电脑上面的控制面板 找到这两个文 ...
- adult
adult是adolescere (grow up)的过去分词. egg - embryo [胚胎] - foetus [就要出生的胎儿] - toddler [刚会走路] - adolescent ...
- A Child's History of England.29
You have not forgotten the New Forest which the Conqueror made, and which the miserable people whose ...
- 在idea的java开发中字符串length()方法获取长度与赋值不符的问题
最近在开发中用到length()方法获取中文字符串的长度,发现获得的长度与实际不符.比如个String类型赋值为"中",但获取长度却是2. 这让我百思不得其解,后来突然想起来我在研 ...
- ssh : connect to host XXX.XXX.XXX.XXX port : 22 connect refused
初学者 写博客 如有不对之处请多多指教 我是要在俩个主机的俩个虚拟机上 用scp (security copy)进行文件远程复制. 但是 终端 提示 ssh : connect to host XXX ...
- Oracle中的null与空字符串''的区别
含义解释:问:什么是NULL?答:在我们不知道具体有什么数据的时候,也即未知,可以用NULL,我们称它为空,ORACLE中,含有空值的表列长度为零.ORACLE允许任何一种数据类型的字段为空,除了以下 ...