Ugly Number II leetcode java
问题描述:
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.
分析:编写程序,找到第n个ugly number。
//动态规划方法
//根据丑数的定义,丑数应该是另一个丑数乘以2、3或者5的结果(1除外)
/**
* 因此我们可以创建一个数组,里面的数字是排好序的丑数,里面的每一个丑数是前面的丑数乘以2、3或者5得到的。
* 关键就是保证数组里面的数字是排好序的。
* 假设arr[1..i]是已经排好序的数组,则arr[i]一定是这里面最大的数,那么我们只要去寻找新生成的数字中比arr[i]大的的最小的数。
* 新生成的数是由前面的数字*2或*3或*5得到的。我们定义index2为前面数字*2中的所有数字中满足大于arr[i]的最小的数的下标,index3,index5类似定义,
* 则应该放在arr[i+1]位置的数字便是min(arr[index2]*2,arr[index3]*3,arr[index5]*5)。
* index2,index3,index5是维持动态向前的,不会产生无效搜索,因为当前找的数字一定比原来找的要大,所以从上一次找到的下标开始进行搜索就可以了。
算法:
- public static int findTheNthUglyNumber(int Mindex){
- int index = 1;
- int[] arr = new int[Mindex]; //存放排好序的ugly number
- arr[0] = 1; //最小ugly number
- int index2 = 0, index3 = 0, index5 = 0; //index2定义为前面数字*2的所有数字中满足大于arr[i]的最小数的下标,index3和index5的定义类似。
- while(index < Mindex)
- {
- int min = Min(arr[index2] * 2,arr[index3] * 3,arr[index5] * 5); //这里要注意 乘以2,3,5
- arr[index] = min;
//更新index2,index3,index5- while(arr[index2] * 2 <= arr[index]) index2++;
- while(arr[index3] * 3 <= arr[index]) index3++;
- while(arr[index5] * 5 <= arr[index]) index5++;
- index++;
- }
- // int ans = arr[Mindex - 1]; //获得第n个ugly number
- return arr[index - 1 ];
- }
- //寻找三者中的最小值
- public static int Min(int a, int b , int c){
- a = a < b ? a : b;
- if(c < a) return c;
- else return a;
- }
Ugly Number II leetcode java的更多相关文章
- Ugly Number II -- LeetCode
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- Single Number II leetcode java
问题描述: Given an array of integers, every element appears three times except for one. Find that single ...
- Leetcode之动态规划(DP)专题-264. 丑数 II(Ugly Number II)
Leetcode之动态规划(DP)专题-264. 丑数 II(Ugly Number II) 编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n ...
- 【LeetCode】264. Ugly Number II
Ugly Number II Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose ...
- 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] 264. Ugly Number II (medium)
263. Ugly Number的子母题 题目要求输出从1开始数,第n个ugly number是什么并且输出. 一开始想着1遍历到n直接判断,超时了. class Solution { public: ...
- 【刷题-LeetCode】264. Ugly Number II
Ugly Number II Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose ...
- Ugly Number,Ugly Number II,Super Ugly Number
一.Ugly Number Write a program to check whether a given number is an ugly number. Ugly numbers are po ...
- 【LeetCode】264. Ugly Number II 解题报告(Java & Python)
标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ https://leetcode.com/prob ...
随机推荐
- Pollard Rho大质数分解学习笔记
目录 问题 流程 代码 生日悖论 end 问题 给定n,要求对n质因数分解 普通的试除法已经不能应用于大整数了,我们需要更快的算法 流程 大概就是找出\(n=c*d\) 如果\(c\)是素数,结束,不 ...
- R read.tabe line 5 did not have 2 elements
R read.tabe line 5 did not have 2 elements Reason: there are special characters such as # in file o ...
- LOJ121 「离线可过」动态图连通性
思路 动态图连通性的板子,可惜我不会在线算法 离线可以使用线段树分治,每个边按照存在的时间插入线段树的对应节点中,最后再dfs一下求出解即可,注意并查集按秩合并可以支持撤销操作 由于大量使用STL跑的 ...
- Linux 压缩、解压命令使用
tar在Linux上是常用的打包.压缩.加压缩工具,他的参数很多,这里仅仅列举常用的压缩与解压缩参数 参数: -c :create 建立压缩档案的参数:(个人理解也就是打包) -x : 解压缩压缩档案 ...
- Android四种布局方式
线性布局 <LinearLayout android:layout_width="match_parent" android:layout_height="wrap ...
- Python学习 day03打卡
今天学习的主要内容: ppython的基本数据类型: 1. python基本数据类型回顾 2.int---数字类型 4.str---字符串类型 一.python基本数据类型 1. int==>整 ...
- hdu 3094 A tree game 树上sg
A tree game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Prob ...
- 实现一个键对应多个值的字典(multidict)
一个字典就是一个键对应一个单值的映射.如果你想要一个键映射多个值,那么你就需要将这多个值放到另外的容器中, 比如列表或者集合里面.比如,你可以像下面这样构造这样的字典: d = { , , ], , ...
- 解决UnicodeEncodeError: 'gbk' codec can't encode character u'\u25aa' in position 344 : illegal multiby
Python拿来做爬虫的确很不错,但是字符串的编码的确是稍不留神就是一个坑,GBK编码和Unicode编码的转化出现问题也是很多的,今天在解析网页数据的时候出现上述错误,解决方案如下: one_str ...
- sqlserver 中常见的函数字符串函数
---字符中操作函数 UPPER(S) 将字符串统一为大写字母 SELECT UPPER('asasA') --ASASA LOWER(S) 将字符串统一为小写字母 SELECT LOWER('asa ...