编写程序找第 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实现:

  1. class Solution {
  2. public int nthUglyNumber(int n) {
  3. if(n<=1){
  4. return n;
  5. }
  6. int[] res=new int[n];
  7. res[0]=1;
  8. int i2=0,i3=0,i5=0;
  9. for(int i=1;i<n;++i){
  10. res[i]=Math.min(res[i2]*2,Math.min(res[i3]*3,res[i5]*5));
  11. if(res[i]==res[i2]*2){
  12. ++i2;
  13. }
  14. if(res[i]==res[i3]*3){
  15. ++i3;
  16. }
  17. if(res[i]==res[i5]*5){
  18. ++i5;
  19. }
  20. }
  21. return res[n-1];
  22. }
  23. }

C++实现:

  1. class Solution {
  2. public:
  3. int nthUglyNumber(int n) {
  4. if(n<=1)
  5. {
  6. return n;
  7. }
  8. vector<int> res(n);
  9. res[0]=1;
  10. int i2=0,i3=0,i5=0;
  11. for(int i=1;i<n;++i)
  12. {
  13. res[i]=min(min(res[i2]*2,res[i3]*3),res[i5]*5);
  14. if(res[i]==res[i2]*2)
  15. {
  16. ++i2;
  17. }
  18. if(res[i]==res[i3]*3)
  19. {
  20. ++i3;
  21. }
  22. if(res[i]==res[i5]*5)
  23. {
  24. ++i5;
  25. }
  26. }
  27. return res[n-1];
  28. }
  29. };

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. 欧拉 路径&&回路

    不管 欧拉回路  还是  欧拉路径  无向图或者有向图(删除方向后)要联通 欧拉路径存在的判定条件 1   无向图   度数为奇数的点最多有两个 2   有向图   最多只能有两个点的入度不等于出度  ...

  2. MongoDB学习day02--数据库增删改查

    (window系统,在cmd命令提示符中使用) 一.数据库使用 管理mongodb数据库:mongo,连接本地数据库,或mongo 127.0.0.1:27017,连接其他服务器:mongo  ip: ...

  3. spring 数据源JNDI 基于tomcat mysql配置

    关键代码 <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean&q ...

  4. 条款三:尽量用new和delete而不用malloc和free

    malloc和free(及其变体)会产生问题的原因在于它们太简单:他们不知道构造函数和析构函数. 假设用两种方法给一个包含10个string对象的数组分配空间,一个用malloc,另一个用new: s ...

  5. C#如何编辑tab选项卡

    1 左侧的TabControl   2 如果要添加或删除选项卡个数,则定位到整个TabControl,然后编辑TabPages  

  6. UVa 10950 - Bad Code

    题目:有一种编码方式.串仅仅有小写字母构成,每一个小写字母相应一个数字,如今给你妆化后的数字串, 问有多少个原串与之相应,注意数字串里可能有一个前导0. 分析:搜索.按字母顺序存储映射表,按字母顺序匹 ...

  7. SUSE Linux源代码编译安装MySQL 5.6

    这篇文章主要介绍了SUSE Linux下源代码编译方式安装MySQL 5.6过程分享,本文使用SUSE Linux Enterprise Server 10 SP3 (x86_64)系统,须要的朋友能 ...

  8. MySQL-导入与导出

    CSV文件导入MySQL LOAD DATA INFILE语句允许您从文本文件读取数据,并将文件的数据快速导入数据库的表中. 导入文件操作之前,需要准备以下内容: 一.将要导入文件的数据对应的数据库表 ...

  9. swift 笔记 (二十一) —— 高级运算符

    高级运算符 位运算符 按位取反: ~ 按位与运算:  & 按位或运算:  | 按位异或运算:  ^ 按位左移运算: << 按位右移动算: >> 溢出运算符 自从swif ...

  10. HDU 5411 CRB and puzzle (Dp + 矩阵高速幂)

    CRB and Puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) T ...