https://leetcode.com/problems/ugly-number/

Write a program to check whether a given number is an ugly number.

Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7.

Note that 1 is typically treated as an ugly number.

class Solution {
public:
bool isUgly(int num) {
if(num <= ) return false;
if(num == ) return true; while(num% == ){
num/=;
if(num == ) return true;
}
while(num% == ){
num/=;
if(num == ) return true;
}
while(num% == ){
num/=;
if(num == ) return true;
}
return false;
}
};

leetcode 263: Ugly Numbers

https://leetcode.com/problems/ugly-number-ii/

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.

Hint:

  1. The naive approach is to call isUgly for every number until you reach the nth one. Most numbers are not ugly. Try to focus your effort on generating only the ugly ones.
  2. An ugly number must be multiplied by either 2, 3, or 5 from a smaller ugly number.
  3. The key is how to maintain the order of the ugly numbers. Try a similar approach of merging from three sorted lists: L1, L2, and L3
class Solution {
public:
int nthUglyNumber(int n) {
if(n <= ) return n; vector<long long> dp(n+, );
for(int i=;i<=;++i) dp[i] = i; for(int i=;i<=n;++i) {
long long Min = numeric_limits<long long>::max();
for(int pre=;pre<=i-;++pre) {
if(dp[pre]* > dp[i-]) {Min = min(Min, dp[pre] * ); continue;}
else if(dp[pre] * > dp[i-]) {Min = min(Min, dp[pre] * );continue;}
else if(dp[pre] * > dp[i-]) Min = min(Min, dp[pre] * );
}
dp[i] = Min;
} return dp[n];
}
};

leetcode 264: Ugly Numbers II

leetcode@ [263/264] Ugly Numbers & Ugly Number II的更多相关文章

  1. 【Leetcode】【Medium】Single Number II

    Given an array of integers, every element appears three times except for one. Find that single one. ...

  2. 【leetcode刷题笔记】Single Number II

    Given an array of integers, every element appears three times except for one. Find that single one. ...

  3. 136 - Ugly Numbers

     Ugly Numbers  Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3 ...

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

  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 丑陋数 II

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

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

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

  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] 264. Ugly Number II (medium)

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

随机推荐

  1. hdu 4195

    我本来的想法求这个三角形的外心~~ 可以得到三条边对应圆心角   则这个正多边形的一条边对应的圆心角 应可以整除这三个角 但是一开始 没想到暴力枚举边数n,  还用模板求圆心坐标  然后求圆心角   ...

  2. IIC驱动分析

    IIC设备是一种通过IIC总线连接的设备,由于其简单性,被广泛引用于电子系统中.在现代电子系统中,有很多的IIC设备需要进行相互之间通信 IIC总线是由PHILIPS公司开发的两线式串行总线,用于连接 ...

  3. eclipse 安装配置maven

    1.安装maven 插件 从eclipse 3.7(indigo)之后,m2e 插件已host到eclipse.org 站点下: Since Eclipse 3.7 (Indigo), m2e is ...

  4. navicat 导入sql文件乱码问题解决

    一,右键数据库链接,点击链接属性,修改以下信息,如图: 选择高级选项页签==>去掉使用MySQL字符集复选框==>选择GB2312字符编码==>点击确定 三,控制台后,show va ...

  5. linux动态库默认搜索路径设置的三种方法

    众所周知, Linux 动态库的默认搜索路径是 /lib 和 /usr/lib .动态库被创建后,一般都复制到这两个目录中.当程序执行时需要某动态库, 并且该动态库还未加载到内存中,则系统会自动到这两 ...

  6. 【转】PostgreSQL IP地址访问配置

    原文:http://blog.csdn.net/shuaiwang/article/details/1793294 1.PostgreSQL的安装目录,进入data文件夹,打开postgresql.c ...

  7. Oracle程序包

    程序包由两部分构成:规范(specification)和主体(body). 创建表 create table PEOPLE ( ID NUMBER primary key not null, NAME ...

  8. ~/.ctag的作用与配置

    里边可以有基本配置和语言正则表达式解析的参数 # Basic options --recurse=yes --tag-relative=yes --exclude=.git # Regex for C ...

  9. JQUERY 选择器 总结,比较全

    jQuery 的选择器可谓之强大无比,这里简单地总结一下常用的元素查找方法 $("#myELement") 选择id值等于myElement的元素,id值不能重复在文档中只能有一个 ...

  10. 过虑器 ThreadLocal 权限 监听器 观察者模式

    数据的压缩 GzipOutputStream - > > ByteArrayOutputStream. 以下是在某个servlet中对指定的数据进行压缩 package cn.itcast ...