题目:

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. MyBatis 基础搭建及架构概述

    目录 MyBatis 是什么? MyBatis 项目构建 MyBatis 整体架构 接口层 数据处理层 基础支持层 MyBatis 是什么? MyBatis是第一个支持自定义SQL.存储过程和高级映射 ...

  2. DRF 视图

    目录 一.DRF中的Request 二.前戏: 关于面向对象的继承 三.初级版本 1. settings.py文件 -- 注册app 2. models.py文件 -- 创建表 3. admin.py ...

  3. C++中 =default,=delete用法

    =default: 用于显式要求编译器提供合成版本的四大函数(构造.拷贝.析构.赋值) 例如: class A{ public: A() = default; A(const A& a) = ...

  4. .Net Core Api 授权认证

    一.所使用到的NuGet: 1. System.IdentityModel.Tokens.Jwt 2. Microsoft.AspNetCore.Authentication.JwtBearer 二. ...

  5. Java设计模式学习笔记(四) 抽象工厂模式

    前言 本篇是设计模式学习笔记的其中一篇文章,如对其他模式有兴趣,可从该地址查找设计模式学习笔记汇总地址 1. 抽象工厂模式概述 工厂方法模式通过引入工厂等级结构,解决了简单工厂模式中工厂类职责太重的问 ...

  6. 赛前集训的第二个小总结(OI生涯盛极必衰orNOIP前最后试炼?)+关于学OI目的的思考

    这次只有成绩统计是全的…… 看上去好像还是挺跌宕起伏的啊…… 话说亲爱的教练员又一次食言了,说好的10次就换呢?! 名次表只有前10次的了…… 不得不说前10次还是挺好的,10次考试,7次前十,5次前 ...

  7. 使用flink Table &Sql api来构建批量和流式应用(2)Table API概述

    从flink的官方文档,我们知道flink的编程模型分为四层,sql层是最高层的api,Table api是中间层,DataStream/DataSet Api 是核心,stateful Stream ...

  8. 批量替换git目录的远程仓库URL地址脚本

    需求: 1. 输入work-dir 工作目录 2. 扫描工作目录中的子目录 3. 对每一个子目录, 判断是否是git repo 4. 确认是git repo, 获取git origin remote- ...

  9. 下载历史版本CentOS

    搜索centos 进入主页面向下移动滚动找到 点击后向下移动,选择需要的版本进行tree 选择 OK!

  10. 程序员到sql笔记

    1最近准备面试,总结一下之前学过到东西.