leetcode@ [263/264] Ugly Numbers & Ugly Number II
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:
- The naive approach is to call 
isUglyfor every number until you reach the nth one. Most numbers are not ugly. Try to focus your effort on generating only the ugly ones. - An ugly number must be multiplied by either 2, 3, or 5 from a smaller ugly number.
 - 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的更多相关文章
- 【Leetcode】【Medium】Single Number II
		
Given an array of integers, every element appears three times except for one. Find that single one. ...
 - 【leetcode刷题笔记】Single Number II
		
Given an array of integers, every element appears three times except for one. Find that single one. ...
 - 136 - Ugly Numbers
		
Ugly Numbers Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3 ...
 - 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 ...
 - [LeetCode] 264. Ugly Number II 丑陋数之二
		
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
 - [LeetCode] 264. Ugly Number II 丑陋数 II
		
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
 - Leetcode之动态规划(DP)专题-264. 丑数 II(Ugly Number II)
		
Leetcode之动态规划(DP)专题-264. 丑数 II(Ugly Number II) 编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n ...
 - 【LeetCode】264. Ugly Number II
		
Ugly Number II Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose ...
 - [leetcode] 264. Ugly Number II (medium)
		
263. Ugly Number的子母题 题目要求输出从1开始数,第n个ugly number是什么并且输出. 一开始想着1遍历到n直接判断,超时了. class Solution { public: ...
 
随机推荐
- Manifest 与TypeTag
			
Manifest和TypeTag是要解决什么问题? As with other JVM languages, Scala’s types are erased at compile time. T ...
 - SetTimer在无窗口和有窗口线程的使用 . .
			
今天犯了一个粗心的错误,在无窗口线程中,SetTimer中设置计时器ID,而WM_TIMER消息响应函数中得到的计时器ID却不是之前设置的计时器ID. 对应计时器ID的输出的是一个随机数字. 原来在m ...
 - jmeter 测试java协议经验总结
			
对java协议的良好支持,是jmeter比loadrunner优秀的地方,但是坑也不少,本文将相关点都整理下来备忘 一. 依赖的jar包 使用IDE开发jemter java协议脚本时,需要导入以下几 ...
 - 李洪强漫谈iOS开发[C语言-011] - C语言标示符
			
/** * 标示符 2016年 7月 14日 01 低级语言和高级语言的最大不同: 低级语言用的是机器指令 高级语言就是写一些人可以看得懂的代码-汇编语言 标示符 就是名字 命名规则: 1) 只 ...
 - javaweb学习总结(四十七)——监听器(Listener)在开发中的应用
			
监听器在JavaWeb开发中用得比较多,下面说一下监听器(Listener)在开发中的常见应用 一.统计当前在线人数 在JavaWeb应用开发中,有时候我们需要统计当前在线的用户数,此时就可以使用监听 ...
 - SQL Server常见问题总结
			
写在前面 在QQ群,微信群,论坛中经常帮助使用SQL Server数据库的朋友解决问题,但是有一些最常见最基本的问题,每天都有人问,回答多了也不想再解答了,索性把这些问题整理一下,再有人问到直接发链接 ...
 - uva 993 Product of digits (贪心 + 分解因子)
			
Product of digits For a given non-negative integer number N , find the minimal natural Q such tha ...
 - chrome禁用某个网站js脚本的执行
			
1 首先打开谷歌浏览器.如下 2 点击右上角,打开菜单进入[设置] 3 打开后,第一个界面是没有这个的,要滚动到最后点击[显示高级设置...] 4 展开第二页后,点击[隐私设置]->[内容设 ...
 - 创业实战go语言制作网站(转)
			
简单介绍一下之前的整个职业生涯,挺典型的,工程师 –> 资深工程师 –> 架构师 –> 项目经理 –> 部门经理,可以说,产品研发过程中的所有角色我都走了一遍,任职的公司也越来 ...
 - UpYun上传 401 Unauthorized
			
_upt=3b9b444a14059041252014-07-21 08:46:25,218 ERROR (com.UpYun:518) - Upload file error:<h1>4 ...