【Leetcode】264. 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, and n does not exceed 1690.
Show Hint
Hint:The naive approach is to call isUgly for every number until you reach the nth one. Most numbers are not ugly. Try to focus your effort on generating only the ugly ones.
An ugly number must be multiplied by either 2, 3, or 5 from a smaller ugly number.
The key is how to maintain the order of the ugly numbers. Try a similar approach of merging from three sorted lists: L1, L2, and L3.
Assume you have Uk, the kth ugly number. Then Uk+1 must be Min(L1 * 2, L2 * 3, L3 * 5).
思路
不能够依次去判断每个数是不是丑数,这样时间复杂度会特别高。所以只能根据丑数来求下一个丑数。
我们知道丑数都是由已有的丑数乘以2、3、5得到的,所以核心目标就变成了找到下一个丑数,即从当前丑数里找到一个数,乘以2或3或5之后刚好大于当前最大丑数。于是,我们可以维护三个下标,分别记录A、B、C三个数,这三个数有这样的特点:
A*2 > maxUgly, B*3 > maxUgly, C*5 > maxUgly
然后我们再比较A*2, B*3和C*5哪个最小,这样它就是当前最大丑数了!
另外,每次求得当前最大丑数后,还需要更新A、B、C的下标,利用while循环就可以更新了。
代码
public class Solution {
public int min(int a, int b, int c) {
int t = a < b ? a : b;
return t < c ? t : c;
}
public int nthUglyNumber(int n) {
if(n <= 0) return -1;
//创建一个大小为n的数组存储前n个丑数
int[] u = new int[n];
u[0] = 1;
//创建三个下标分别记录第一个乘以2、3、5的乘积大于当前最大的丑数的下标
int index2 = 0;
int index3 = 0;
int index5 = 0;
//记录当前丑数个数
int cur = 1;
//记录当前最大的丑数的下标
int maxIndex = 0;
while(cur < n) {
//求丑数中乘以2、3、5中最小的数,作为下一个丑数
int m = min(u[index2] * 2, u[index3] * 3, u[index5] * 5);
u[cur++] = m;
//更新最大丑数下标
maxIndex++;
//更新index2,index3和index5,使得它们与2、3、5的乘积刚好大于当前最大丑数
while (u[index2]*2 <= u[maxIndex]) index2++;
while (u[index3]*3 <= u[maxIndex]) index3++;
while (u[index5]*5 <= u[maxIndex]) index5++;
}
return u[n-1];
}
}
【Leetcode】264. Ugly Number II ,丑数的更多相关文章
- [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 丑陋数之二
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- 264 Ugly Number II 丑数 II
编写程序找第 n 个丑数.丑数就是只包含质因子 2, 3, 5 的正整数.例如, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 就是前10个丑数.注意:1. 1 一般也被当做丑数2. ...
- [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 fact ...
- 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 ...
- Leetcode264. Ugly Number II丑数2
编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n = 10 输出: 12 解释: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 ...
- leetcode 263. Ugly Number 、264. Ugly Number II 、313. Super Ugly Number 、204. Count Primes
263. Ugly Number 注意:1.小于等于0都不属于丑数 2.while循环的判断不是num >= 0, 而是能被2 .3.5整除,即能被整除才去除这些数 class Solution ...
- [LeetCode] Super Ugly Number 超级丑陋数
Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...
随机推荐
- [Luogu 3958] NOIP2017 D2T1 奶酪
题目链接 人生第一篇题解,多多关照吧. 注意事项: 1.多组数据,每次要先初始化. 2.因为涉及到开根,所以记得开double. 整体思路: 建图,判断「起点」与「终点」是否连通. 方法可选择搜索(我 ...
- 深入浅出MyBatis:JDBC和MyBatis介绍
JDBC相关概念 Java程序都是通过JDBC连接数据库的,通过SQL对数据库编程,JDBC是由SUN公司提出的一些列规范,只定义了接口规范,具体实现由各个数据库厂商去实现,它是一种典型的桥接模式. ...
- [IOS]Xcode各版本官方下载及百度云盘下载, Mac和IOS及Xcode版本历史
官方下载, 用开发者账户登录,建议用Safari浏览器下载. 官方下载地址: https://developer.apple.com/xcode/downloads/ 百度云盘下载地址 http:// ...
- bzoj 2786 DP
我们可以将=左右的两个数看成一个块,块内无顺序要求,把<分隔的看成两个块,那么我们设w[i][j]代表将i个元素分成j个块的方案数,那么显然w[i][j]=w[i-1][j]*j+w[i-1][ ...
- MFC单文档框架分析及执行流程(转)
原文转自 https://blog.csdn.net/u011619422/article/details/40402705 首先来分析一下MFC单文档类的结构: 它包括如下几个类: CAboutDl ...
- C++学习之路(二):引用
(1)引用是变量的别名 引用的基本定义格式:类型 &引用名 = 变量名 例如:int a = 1; int &b = a,这里b是a的别名,b与a都指向了同一块内存单元. 对于引用而言 ...
- SVN服务的配置与管理
引言 没当服务器重启,SVN服务都会停止,每次都得重启一下服务,为了解决这样的问题.有了下文. 一.配置自启动服务 sc create SVNService binpath="E:\svn\ ...
- 使用Webpack搭建Vue项目
前提: 1. 借助Node.js环境里的npm来安装, 2. 设置好npm镜像, (比如淘宝的npm镜像:输入 引用 npm install -g cnpm –registry=https://r ...
- Median_of_Two_Sorted_Arrays(理论支持和算法总结)
可以将这个题目推广到更naive的情况,找两个排序数组中的第K个最大值(第K个最小值). 1.直接 merge 两个数组,然后求中位数(第K个最大值或者第K个最小值),能过,不过复杂度是 O(n + ...
- 取消div,a等标签点击效果
当标签被设置onclick事件之后,在有些手机浏览器中,点击这些标签,会有点击变色效果.想要取消点击变色效果. 添加:div{-webkit-tap-highlight-color:rgba(0,0, ...