题目:

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://blog.csdn.net/u012243115/article/details/45222269

代码:

class Solution {
public:
int nthUglyNumber(int n)
{
if(n <= 0)
return 0;
int *uglyNum = new int[n]();
int *uglyNum2 = uglyNum ;
int *uglyNum3 = uglyNum ;
int *uglyNum5 = uglyNum ;
uglyNum[0] = 1;
int count = 1;
while(count < n)
{
int curUgly = min(min(*uglyNum2 * 2 , *uglyNum3 * 3) , *uglyNum5 * 5);
uglyNum[count] = curUgly;
while(*uglyNum2 * 2 <= curUgly)
uglyNum2++;
while(*uglyNum3 * 3 <= curUgly)
uglyNum3++;
while(*uglyNum5 * 5 <= curUgly)
uglyNum5++;
count++;
}
int result = uglyNum[n-1];
delete [] uglyNum;
return result; }
};

LeetCode OJ 之 Ugly Number II (丑数-二)的更多相关文章

  1. LeetCode OJ:Ugly Number(丑数)

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

  2. LeetCode OJ 之 Ugly Number (丑数)

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

  3. LeetCode OJ:Ugly Number II(丑数II)

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

  4. 264 Ugly Number II 丑数 II

    编写程序找第 n 个丑数.丑数就是只包含质因子 2, 3, 5 的正整数.例如, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 就是前10个丑数.注意:1. 1 一般也被当做丑数2. ...

  5. [LeetCode]313. Super Ugly Number超级丑数,丑数系列看这一道就行了

    丑数系列的题看这一道就可以了 /* 和ugly number2差不多,不过这次的质因子多了,所以用数组来表示质因子的target坐标 target坐标指的是这个质因子此次要乘的前任丑数是谁 */ pu ...

  6. Leetcode264. Ugly Number II丑数2

    编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n = 10 输出: 12 解释: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 ...

  7. [LeetCode] 264. Ugly Number II 丑陋数 II

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

  8. [LeetCode] 313. Super Ugly Number 超级丑陋数

    Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...

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

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

随机推荐

  1. VS2015企业版序列号

    vs2015 企业版HM6NR-QXX7C-DFW2Y-8B82K-WTYJV2XNFG-KFHR8-QV3CP-3W6HT-683CH

  2. 约瑟夫环C#解决方法

    /*约瑟夫环 (问题描述) 约瑟夫问题的一种描述是:编号为1,2,......n,的n个人按顺时针方向围坐一圈,每个人持有一个密码(正整数).一开始任意选 一个正整数作为报数的上限值m,从第一个人开始 ...

  3. Transact-SQL知识点梳理

    Transact-SQL基础语言 运行环境:SQL Server 语法约定: 语法约定 用途说明 大写字母 Transact-SQL关键字 斜体 用户提供的Transact-SQL语法参数 粗体 数据 ...

  4. [转载] Quartz作业调度框架

    转载自http://yangpanwww.iteye.com/blog/797563 Quartz 是一个开源的作业调度框架,它完全由 Java 写成,并设计用于 J2SE 和 J2EE 应用中.它提 ...

  5. 入坑第二式 golang入坑系统

    史前必读: 这是入坑系列的第二式,如果错过了第一式,可以去gitbook( https://andy-zhangtao.gitbooks.io/golang/content/ )点个回放,看个重播.因 ...

  6. python requests库学习笔记(上)

    尊重博客园原创精神,请勿转载! requests库官方使用手册地址:http://www.python-requests.org/en/master/:中文使用手册地址:http://cn.pytho ...

  7. Vuejs-组件-<slot> 标签分发内容

    资料来自:https://cn.vuejs.org/v2/guide/components.html#具名-Slot 在官方文档的基础上,更加细致的讲解代码. <slot> 标签中的任何内 ...

  8. eclipse中Maven工程使用Tomcat7以上插件

    Maven中使用tomcat:run命令默认是使用Tomcat6的版本, 现在要用到Tomcat7以上的版本,在eclipse的Maven工程中配置如下 第一步:在项目的pom里面加入如下配置: 官网 ...

  9. 查看.ssh文件在哪

    输入命令 ll -d ~/.ssh 后你就都明白了.

  10. 谈谈form-data请求格式

    最近一直都比较忙,坚持月月更新博客的计划不得中止了,今天好不容易抽出点时间来说说最近项目中遇到的一个问题,有关request post请求格式中的multipart/form-data格式. 引言 最 ...