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. 网页优化URI(http URI scheme与data URI scheme)

    网页优化的一大首要任务是减少HTTP 请求 (http request) 的次数,例如通过合并多个JS文件,合并CSS样式文件.除此之外,还有一个data URL 的密技,让我们直接把图像的内容崁入网 ...

  2. Codeforce385C 树状数组+素因子分解

    题目大意: 给多个区间的询问,在询问区间内每一个出现的素数去计算所有数中有多少个数能被这个素数整除 然后将所有素数得到的对应值求和 这里因为初始给定的数不超过10000000,最多670000不到的素 ...

  3. JAVA学习1

    以前学过JAVA,但是长时间不用又给忘了,趁着还有时间回顾一下. 一切皆是对象.

  4. [安卓]softap

    http://www.cnblogs.com/javawebsoa/archive/2013/05/29/3106758.html

  5. 深入理解mybatis参数

    这个的话我是看的别人的文章,感觉很好: http://blog.csdn.net/isea533/article/details/44002219

  6. ThinkPHP中Session用法详解

    在ThinkPHP封装了Session类,用户可以直接使用,常用的方法有: Session::set(name, value):注册 session . Session::is_set(name):检 ...

  7. 《用格式化(fprintf和fscanf函数)的方式读写文件》

    //用格式化(fprintf和fscanf函数)的方式读写文件 [用格式化的方式向文件中写入数据]#include<stdio.h>#include<stdlib.h> int ...

  8. PAT 06-2 字符串字母大小写转换

    没什么好说的,记得使用ctype.h就好了,谭浩强那本书就介绍了,再不使用就太对不起他老人家了:有一点小小的地方需要注意一下,&&的优先级比=号高,所以getchar()两边没有括号的 ...

  9. JS 基础事件的用法

    // 1.9以上用on // 案例一 // $('#btn').on('click', function(){ // //console.log(1); // alert('测试...'); // } ...

  10. ASP.NET中把xml转为dataset与xml字符串转为dataset及dataset转为xml的代码

    转自:http://www.cnblogs.com/_zjl/archive/2011/04/08/2009087.html XmlDatasetConvert.csusing System;usin ...