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

详见:https://leetcode.com/problems/ugly-number-ii/description/

Java实现:

class Solution {
public int nthUglyNumber(int n) {
if(n<=1){
return n;
}
int[] res=new int[n];
res[0]=1;
int i2=0,i3=0,i5=0;
for(int i=1;i<n;++i){
res[i]=Math.min(res[i2]*2,Math.min(res[i3]*3,res[i5]*5));
if(res[i]==res[i2]*2){
++i2;
}
if(res[i]==res[i3]*3){
++i3;
}
if(res[i]==res[i5]*5){
++i5;
}
}
return res[n-1];
}
}

C++实现:

class Solution {
public:
int nthUglyNumber(int n) {
if(n<=1)
{
return n;
}
vector<int> res(n);
res[0]=1;
int i2=0,i3=0,i5=0;
for(int i=1;i<n;++i)
{
res[i]=min(min(res[i2]*2,res[i3]*3),res[i5]*5);
if(res[i]==res[i2]*2)
{
++i2;
}
if(res[i]==res[i3]*3)
{
++i3;
}
if(res[i]==res[i5]*5)
{
++i5;
}
}
return res[n-1];
}
};

264 Ugly Number II 丑数 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]313. Super Ugly Number超级丑数,丑数系列看这一道就行了

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

  3. 【easy】263. Ugly Number 判断丑数

    class Solution { public: bool isUgly(int num) { ) return false; ) return true; && num % == ) ...

  4. LeetCode OJ 之 Ugly Number (丑数)

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

  5. 313 Super Ugly Number 超级丑数

    编写一段程序来寻找第 n 个超级丑数.超级丑数是指其所有质因数都在长度为k的质数列表primes中的正整数.例如,[1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32] ...

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

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

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

  9. leetcode 264. 丑数 II 及 313. 超级丑数

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

随机推荐

  1. sql语句在Mysql中如何执行?

    1.MySQL 主要分为 Server 层和引擎层,Server 层主要包括连接器.查询缓存.分析器.优化器.执行器,同时还有一个日志模块(binlog),这个日志模块所有执行引擎都可以共用,redo ...

  2. SecurityContextHolder.getContext().getAuthentication()为null的情况

    原理: UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication() . ...

  3. Windows Socket IO 模型

    http://www.cppblog.com/huangwei1024/archive/2010/11/22/134205.html

  4. STL之关联容器的映射底层

    STL的关联容器有set, map, multiset, multimap.用于实现它们的底层容器有划入标准的rb_tree和待增加标准的hashtable. 底层容器rb_tree为上层容器提供了一 ...

  5. 推荐IOS开发3个工具:Homebrew、TestFight、Crashlytics-b

    1. Homebrew 什么是Homebrew? Homebrew is the easiest and most flexible way to install the UNIX tools App ...

  6. 工作总结 2018 - 4 - 13 select标签 multiple 属性 同时选择多个选项

    <div class="col-xs-4"> @Html.DropDownList("CustomerType", (MultiSelectList ...

  7. js坑爹笔试题目汇总(持续更新中)

    把你的面试官问倒,你就是一个合格的面试者了,以下总结一些易错的js笔试题目,会持续更新中.欢迎关注 1,考察this var length = 10 function fn(){ alert(this ...

  8. webpack之傻瓜式教程(转载)

    1.安装好nodejs,安装过程网上找. 2.在D盘.E盘或任意一个磁盘中新建文件夹,命名为webpack_demo: 3.在webpack_demo文件下按住Shift键后点击鼠标右键,再左键点击“ ...

  9. iOS开发——高级篇——iOS 强制退出程序APP代码

    1.先po代码 UIAlertView* alert = [[UIAlertView alloc] initWithTitle:self.exitapplication message:@" ...

  10. 【bzoj1149】 [CTSC2007]风玲Mobiles

    题目意为:给一颗二叉树,每一次操作可以交换该子树的左右两颗子树,要将该树变为完全二叉树,求最小操作次数.从根开始进行一遍DFS.记录每棵子树的大小size,如果左子树的size小于右子树的size那么 ...