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的更多相关文章

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

  2. 【LeetCode】264. Ugly Number II

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

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

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

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

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

  5. [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 264. Ugly Number II

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

  7. (medium)LeetCode 264.Ugly Number II

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

  8. 264. Ugly Number II(丑数 剑指offer 34)

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

  9. 【Leetcode】264. Ugly Number II ,丑数

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

随机推荐

  1. bzoj 1856: [Scoi2010]字符串

    #include<cstdio> #include<iostream> #define Q 20100403 ; int main() { scanf("%lld%l ...

  2. linux 线程操作问题undefined reference to 'pthread_create'的解决办法(cmake)

    问题原因: pthread 库不是 Linux 系统默认的库,连接时需要使用静态库 libpthread.a. 所以在使用pthread_create()创建线程时,需要链接该库. 1. 终端:问题解 ...

  3. ASP.NET MVC学习之路由篇(3)

    根据路由输出链接 既然是网站开发自然少不了链接,我们已经学会了强大的路由,但是还缺少一步就是能够将这些路由的路径输出到页面,下面我们就开始学习如何输出路由路径. 首先我们的路由注册部分如下所示: 1 ...

  4. lucene4.7学习总结

    花了一段时间学习lucene今天有时间把所学的写下来,网上有很多文章但大部分都是2.X和3.X版本的(当前最新版本4.9),希望这篇文章对自己和初学者有所帮助. 学习目录 (1)什么是lucene ( ...

  5. GoldenGate中使用strcat和strext进行数据转换

    在OGG中可以对源字段的内容进行合并或拆分,从而实现类似于“ETL”的功能.strcat(s1,s2,s3,,,):用于合并字串:strext(str, start, end):用于获取指定位置的字串 ...

  6. Linux访问Windows磁盘实现共享

    业务需求说明:公司在部署hadoop集群和DB server与SAN存储,公司的想法是前端通过DB Server能够将非结构化的数据能放进SAN存储当中,而hadoop集群也能够访问这个SAN存储.因 ...

  7. HDU 3829 - Cat VS Dog (二分图最大独立集)

    题意:动物园有n只猫和m条狗,现在有p个小孩,他们有的喜欢猫,有的喜欢狗,其中喜欢猫的一定不喜欢狗,喜欢狗的一定不喜欢猫.现在管理员要从动物园中移除一些动物,如果一个小孩喜欢的动物留了下来而不喜欢的动 ...

  8. WIN中SharePoint Server 2010 入门安装部署详解

    目前流行的原始安装文件基本都是这样的:Windows Server 2008 R2+SQL Server 2008R2+SharePoint Server 2010     这个初始环境原本也无可厚非 ...

  9. App右上角数字

    IOS7: UIApplication *app = [UIApplication sharedApplication]; // 应用程序右上角数字 app.applicationIconBadgeN ...

  10. 【FreeBuf视频】《安全大咖说》专访知道创宇CTO杨冀龙(watercloud)

    [FreeBuf视频]<安全大咖说>专访知道创宇CTO杨冀龙(watercloud) 发布于 2016/05/16 FreeBuF.COM 杨冀龙,江湖人称watercloud,知道创宇公 ...