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.

这也是今天新加的一道题。挺有意思的。~

这道题如果找不到方法的话会很难,但是也不是太难,只能说算法会很复杂。这里还是需要借助一些数学知识。

这道题的思路借鉴于:http://www.geeksforgeeks.org/ugly-numbers/

经过观察ugly number sequence,我们发现其实可以将这个sequence分解为三个小的sequence。(因为一共三个prime factor嘛)

1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, …

(1) 1×2, 2×2, 3×2, 4×2, 5×2, …

(2) 1×3, 2×3, 3×3, 4×3, 5×3, …

(3) 1×5, 2×5, 3×5, 4×5, 5×5, …

我觉得经过观察上面这三个sequence,应该大家可以找到规律了。

每一个分解出来的sequence就等于原来的ugly number sequence中的每个数乘以2/3/5。

因此我们可以用merge sort依次从这三个分解出的sequence里面依次挑出ugly number。借助Math.min()method。

代码如下。~

public class Solution {
public int nthUglyNumber(int n) {
int[] result=new int[n];
result[0]=1;
int a=0;
int b=0;
int c=0;
int factora=2;
int factorb=3;
int factorc=5;
for(int i=1;i<n;i++){
int min=Math.min(Math.min(factora,factorb),factorc);
result[i]=min;
if(factora==min){
factora=2*result[++a];
}
if(factorb==min){
factorb=3*result[++b];
}
if(factorc==min){
factorc=5*result[++c];
}
}
return result[n-1];
}
}

[LeetCode] Ugly Number II (A New Question Added Today)的更多相关文章

  1. [LeetCode] Ugly Number II 丑陋数之二

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

  2. [LeetCode] Ugly Number II

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

  3. LeetCode() Ugly Number II 背下来!

    一个别人,非常牛逼的思路,膜拜了!orz!!!! vector <int> results (1,1); int i = 0, j = 0, k = 0; while (results.s ...

  4. Leetcode之动态规划(DP)专题-264. 丑数 II(Ugly Number II)

    Leetcode之动态规划(DP)专题-264. 丑数 II(Ugly Number II) 编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n ...

  5. 【LeetCode】264. Ugly Number II

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

  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 (medium)

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

  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] Ugly Number 丑陋数

    Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...

随机推荐

  1. Sina App Engine(SAE)入门教程(10)- Cron(定时任务)使用

    参考资料 SAE Cron说明文档 Cron能干什么? cron 可以定时的触发一个脚本,在sae上最大的频率是一分钟一次.你可以用其来完成自己需要的业务逻辑,例如定期的抓取某些网页完菜信息的采集,定 ...

  2. linux系统快速查看进程pid的方法

    一个很简单的命令,pgrep,可以迅速定位包含某个关键字的进程的pid:使用这个命令,再也不用ps aux 以后去对哪个进程的pid了 一个很简单的命令,pgrep,可以迅速定位包含某个关键字的进程的 ...

  3. java抓取动态生成的网页

    最近在做项目的时候有一个需求:从网页面抓取数据,要求是首先抓取整个网页的html源码(后期更新要使用到).刚开始一看这个简单,然后就稀里哗啦的敲起了代码(在这之前使用过Hadoop平台的分布式爬虫框架 ...

  4. 如何写科技文章的讨论discussion部分

    众所周知,讨论部分是在结合自己的研究结果基础上,对整个文章的结论的提炼和升华.这一部分是整个论文的精,往往点睛作用. 同时,很多杂志要求结果和讨论分开,这也就更突出了写好讨论的重要性. 那么,我们应该 ...

  5. Android之NDK编程(JNI)

    转自:http://www.cnblogs.com/xw022/archive/2011/08/18/2144621.html NDK编程入门--C回调JAVA方法   一.主要流程 1.  新建一个 ...

  6. HBase的Shell操作

    1.进入命令行 bin/hbase shell 2.输入help 查看各种命令组. 命令是分组的,可以执行help 'general'查看general组的命令. 3.常用命令 --显示有哪些表 li ...

  7. 面试题_82_to_87_Date、Time 及 Calendar 的面试题

    82)在多线程环境下,SimpleDateFormat 是线程安全的吗?(答案)不是,非常不幸,DateFormat 的所有实现,包括 SimpleDateFormat 都不是线程安全的,因此你不应该 ...

  8. ASP.NET中POST提交数据并跳转页面

    需求:先Post提交数据,然后跳转到目标页面 找了好久才发现这个神奇的类HttpHelper.原理很简单,利用html的from表单拼接,然后执行 使用方法: NameValueCollection ...

  9. Eclipse @override报错解决 必须覆盖超类方法

    解决办法:Windows->Preferences-->java->Compiler-->compiler compliance level设置成1.6

  10. poj 2373 Dividing the Path

    Dividing the Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2858   Accepted: 1064 ...