题目:

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.

大意:

情给出第n个丑数,丑数是因数只有2,3,5的数,从小到大排列,1,2,3,4,5,6,8,9,10,12是前10个丑陋数字的序列。其中1被定义为丑数。

分析:

丑数的因数只能是2,3,5,所以我这样可以得到

1、丑数是丑数*丑数得到的,一开始的丑数是1、2、3、5。

2、每个丑数刚好可以获得三个丑数,分别乘以2、3、5得到。

3、由于丑数都是整数所以,第n个丑数*2<第n个丑数*3<第n个丑数*5

然后我为了分析起来方便定义了一个名词,通过某个丑数*2、*3、*5获得另一个丑数,这样的过程中某个丑数被我称为底丑数。

4、所以第3条可以增加为相同的底丑数*2获得的丑数最小,不同的不一定。

根据这4个规律我们可以这样,

建立一个数组存储丑数,第一个丑数为1,索引为0。

然后设定3个变量记录底丑数在数组中的索引,所以a=0(*2)、b=0(*3)、c=0(*5)指定1为底丑数。

对比1*2,1*3,1*5可得第二关丑数是2,因为这是是*2获得的,那么将a++,因为这个丑数已经获得过了。这是底丑数分别为2,1,1。

对比2*2,1*3,1*5可得第三个丑数是3,因为这是是*3获得的,那么将b++,因为这个丑数已经获得过了。这是底丑数分别为2,2,1。

对比2*2,2*3,1*5可得第三个丑数是4,因为这是是*2获得的,那么将a++,因为这个丑数已经获得过了。这是底丑数分别为3,2,1。

对比3*2,2*3,1*5可得第三个丑数是5,因为这是是*5获得的,那么将c++,因为这个丑数已经获得过了。这是底丑数分别为3,2,2。

对比3*2,2*3,2*5可得第三个丑数是6,因为这是是*2和*3都可获得的,那么将a++,b++,因为这两个相等的丑数已经获得过了。这是底丑数分别为4,3,2。

对比4*2,3*3,2*5可得第三个丑数是8,因为这是是*2获得的,那么将a++,因为这个丑数已经获得过了。这是底丑数分别为5,2,2。

。。。。。。

总结为代码就是:

    public static int nthUglyNumber(int n) {
if(n <= 0){
return 0;
}
List<Integer> result = new ArrayList<Integer>();
result.add(1);
int a = 0, b = 0, c = 0;
while (result.size() < n){
int next = min(result.get(a) * 2,min(result.get(b)*3, result.get(c)*5));
result.add(next);
if(result.get(a) * 2 == next) a++;
if(result.get(b) * 3 == next) b++;
if(result.get(c) * 5 == next) c++;
}
return result.get(n - 1);
}
static int min(int n,int m) {
if(n >= m){
n =m;
}
return n;
}

LeetCode——264. Ugly Number II的更多相关文章

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

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

  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 丑陋数之二

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

  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. (medium)LeetCode 264.Ugly Number II

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

  6. 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 ...

  7. 【LeetCode】264. Ugly Number II

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

  8. 【刷题-LeetCode】264. Ugly Number II

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

  9. 【LeetCode】264. Ugly Number II 解题报告(Java & Python)

    标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ https://leetcode.com/prob ...

随机推荐

  1. 转: windows系统下mysql出现Error 1045(28000) Access Denied for user 'root'@'localhost'

    windows系统下mysql出现Error 1045(28000) Access Denied for user 'root'@'localhost' 转自 http://zxy5241.space ...

  2. 基于SpringCloud的Microservices架构实战案例-配置文件属性内容加解密

    使用过SpringBoot配置文件的朋友都知道,资源文件中的内容通常情况下是明文显示,安全性就比较低一些.打开application.properties或application.yml,比如mysq ...

  3. ElasticSearch7.2安装

    1.环境 Java -version:java11 centos: 7.2 elasticsearch: 7.2 2.获取压缩包 wget https://artifacts.elastic.co/d ...

  4. py+selenium 报错NameError: name 'NoSuchElementException' is not defined【已解决】

     报错:NameError: name 'NoSuchElementException' is not defined  如图 解决方法: 头部加一句:from selenium.common.exc ...

  5. 和朱晔一起复习Java并发(三):锁(含锁性能测试)

    这个专题我发现怎么慢慢演化为性能测试了,遇到任何东西我就忍不住去测一把.本文我们会大概看一下各种锁数据结构的简单用法,顺便也会来比拼一下性能. 各种并发锁 首先,我们定一个抽象基类,用于各种锁测试的一 ...

  6. c++小游戏——三国杀

    #include<iostream> #include<time.h> #include<stdio.h> #include <stdlib.h> us ...

  7. Excel催化剂开源第34波-SM.MS图床API调用(用POST上传multipart/form-data内容)

    日常做网抓数据,都是以GET请求为主,偶尔遇到需要POST请求的,一般POST的参数只是一串字符串就可以了,通过构造字符串也很容易完成,但此次SM.MS的API接口要求是Content-Type: m ...

  8. 个人永久性免费-Excel催化剂功能第82波-复制粘贴按源区域大小自动扩展收缩目标区域

    日常工作中,复制粘贴的操作,永远是最高频的操作,没有之一,在最高频的操作上,进行优化,让过程更智能,比一天到晚鼓吹人工智能替换人的骇人听闻的新闻来得更实际.此篇带来一点点的小小的改进,让日后无数的复制 ...

  9. MYSQL数据库约束类型

    07.14自我总结 MYSQL数据库约束类型 一.主键约束(primary key) 主键约束要求主键列的数据唯一,并且不能为空.主键分为两种类型:单字段主键和多字段联合主键. 1.单字段主键 写法 ...

  10. [HDOJ] 2026.Max Sum

    2026.Max Sum (c++) Problem Description Consider the aggregate An= { 1, 2, -, n }. For example, A1={1 ...