【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 ...
随机推荐
- spring-boot spring-MVC自动配置
Spring MVC auto-configuration Spring Boot 自动配置好了SpringMVC 以下是SpringBoot对SpringMVC的默认配置:==(WebMvcAuto ...
- kubernetes部署haproxy、keepalived为kube-apiserver做集群
也可以用nginx.keepalived做负载均衡,看大家的需求. # yum -y install haproxy keepalived haproxy的配置文件(三台一样): cat > / ...
- 使用systemd将iptables规则在docker启动后自动导入
编写systemd文件 $ sudo vi /etc/systemd/system/iptables-import.service # /etc/systemd/system/iptables-imp ...
- javaSE高级篇6 — 注解( 附:注解底层解析 ) —— 更新完毕
注解 ---- 英文:annotation 1.注解长什么样子? @xxxxxxx( 一些信息 ) ----- 这个信息可有可无 2.注解可以放在什么地方? 类本身的上面.属性的上面.方法的上面.参数 ...
- C++基本函数的调用优化(构造、拷贝构造、赋值)
合理的函数可提升时间和空间的利用率 //Test1.h #include<iostream> using namespace std; struct ST { private: int a ...
- 【STM8】添加头文件、加入库函数
下面顺便放上STM8L15x-16x-05x的固件库,以及固件库里没有的<stm8l15x_conf.h> 链接打开后,还会发现另外两个文件夹,<src><inc> ...
- Linux基础命令---alias别名
alias Alias不带参数或使用-p选项在标准输出上以"name=value"的形式打印别名列表.当提供参数时,为其值给定的每个名称定义一个别名.值中的尾随空格将导致在扩展别名 ...
- k8s之ansible安装
项目地址:https://github.com/easzlab/kubeasz #:先配置harbor #:利用脚本安装docker root@k8s-harbor1:~# vim docker_in ...
- restful接口文档
1.先理清业务bai流程 2.定义前后端开发的接口规范.比如json的格dao式,url的格式 3.定内义接口文容档,这里的接口文档一般就是对应后台的实体reqVo(调用后台接口<控制器> ...
- 【Java基础】ArrayList初始化操作
要用60个零初始化列表,请执行以下操作: List<Integer> list = new ArrayList<Integer>(Collections.nCopies(60, ...