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 ...
随机推荐
- QQ2013登录报文简单分析(不可用于非法用途)
[NO.1 2013-05-08 00:31:16 046 SEND 115字节]02 31 03 08 25 27 B5 88 6F 91 D2 03 00 00 00 0101 01 00 00 ...
- C++-模板的声明和实现为何要放在头文件中
源: http://blog.csdn.net/lqk1985/archive/2008/10/24/3136364.aspx 如何组织编写模板程序 发表日期: 1/21/2003 12:28:58 ...
- [vijos P1112] 小胖的奇偶
第一次看到这题怎么也不会想到是并查集题目…星期五第一次看到这题,到今天做出来,实在是废了好多功夫.看了很多人的解题都有same和diff数组,我也写了,后来发现不对啊两个数组的话find函数怎么写呢? ...
- CodeForces 468A Program F
Description Little X used to play a card game called "24 Game", but recently he has found ...
- RPI学习--环境搭建_无线网络的连接
这里不讨论无线网卡的驱动问题,假设内核已经支持了该网卡. 发现命令行下添加无线网络比较麻烦,于是利用图形桌面的工具(WiFi Config)先配置好,在回到字符终端,发现网络已经配置好了 查看内网ip ...
- JSON基础使用
1)JSON概念 JSON 是纯文本 JSON 具有“自我描述性”(人类可读) JSON 具有层级结构(值中存在值) JSON 可通过 JavaScript 进行解析 JSON 数据可使用 AJAX ...
- 详解x86、IA-32、IA-64等CPU系列
x86架构起源于Intel公司在1978年推出的8086处理器.8086在1981年为IBM PC所选用,之后x86便成为了个人电脑的标准平台,成为历上最成功的CPU架构.8086是一款16位CPU, ...
- HTML基础学习笔记
一.基本结构 <html>——开始标记 <head></head>——头标记,用来控制布局.编码.特效等内容 <body></body>—— ...
- 主线程中一定不能放耗时操作,必须要开子线程,比如下载文件,不然会不让你拿到输入流--报错显示android.os.NetworkOnMainThreadException
1.必须要开子线程来操作耗时操作,android.os.NetworkOnMainThreadException new Thread(new Runnable() { @Override publi ...
- poj1276 多重背包
//Accepted 1100 KB 47 ms //多重背包 #include <cstdio> #include <cstring> #include <iostre ...