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.
代码如下:(方法一:超时)
public class Solution {
public int nthUglyNumber(int n) {
if(n==1||n==2||n==3)
return n;
int count=3;
int i=4;
for(;count<n;i++)
{
int c=i;
while(c%2==0)
c=c/2;
while(c%3==0)
c=c/3;
while(c%5==0)
c=c/5;
if(c==1)
count++;
}
return i-1;
}
}
方法二:(借鉴的别人的方法)
public class Solution {
public int nthUglyNumber(int n) {
if(n<=1)
return 1;
List<Integer> list1=new ArrayList<>();
List<Integer> list2=new ArrayList<>();
List<Integer> list3=new ArrayList<>();
list1.add(1);
list2.add(1);
list3.add(1);
int min=0;
for(int i=1;i<=n;i++)
{
min=Math.min(Math.min(list1.get(0),list2.get(0)),list3.get(0));
if(list1.get(0)==min) list1.remove(0);
if(list2.get(0)==min) list2.remove(0);
if(list3.get(0)==min) list3.remove(0);
list1.add(min*2);
list2.add(min*3);
list3.add(min*5);
}
return min;
}
}
264. Ugly Number II的更多相关文章
- 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
Ugly Number II Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose ...
- [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 ...
- [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 ...
- 264. Ugly Number II(丑数 剑指offer 34)
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 facto ...
随机推荐
- 二模 (11) day1
第一题: 题目大意:用邻接矩阵给出一棵树(边权非负)上N个节点相互之间的最短路距离,求这棵树所有边权的和. 解题过程: 1.暂时还没想出来,待AC. 第二题: 题目大意:给出一些单词,然后建立Trie ...
- win7 MS SQL SERVER 2000安装
http://blog.chinaunix.net/uid-24398518-id-2156226.html MicrosoftInternetExplorer402DocumentNotSpecif ...
- ajax跨域相关
ajax 技术使用xmlhttprequest组件附送请求时,发送请求的url和本网页的url地址必须在同一个域名下如果需要跨域,可以使用iframe或者<javascript src=&quo ...
- Android 之 JSON操作
Android默认已经集成了操作JSON相关的API,如下所示: 也可以不使用JSON工具类,直接使用字符串拼接. 注意:可以使用字符串来构造JSONArray和JSONObject,这就是JSON解 ...
- JAVA工作方式
public class Hellow { int eyes = 2; int ears = 2; int legs = 4; void run(){ //方法 System.out.println( ...
- JVM-class文件完全解析-方法表集合
方法表集合 前面的魔数,次版本号,主板本号,常量池入口,常量池,访问标志,类索引,父类索引,接口索引集合,字段表集合,那么再接下来就是方法表了. 方法表的构造如同字段表一样,依次包括了访问标志(a ...
- config、make、make install
.config/ .configure (查看该目录下是否有这个文件,如果有makefile,可直接make) 配置 config是一个shell脚本,根据平台的特性生成Makefile文件 ...
- opencv中的Mat类型
Mat类型主要是跟matlab中的数据类型一样.故用起来很方便. Mat最大的优势跟STL很相似,都是对内存进行动态的管理,不需要之前用户手动的管理内存,对于一些大型的开发,有时候投入的lpImage ...
- Note_Master-Detail Application(iOS template)_07_ YJYDetailViewController.m
// YJYDetailViewController.m #import "YJYDetailViewController.h" @interfaceYJYDetailViewC ...
- Python的图形化界面
导入easygui模块有很多种方法 , 这里只介绍一种简单使用的 . import easygui as g 将easygui 简称为g 然后开始调用她的函数就行. import easygui as ...