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
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. - 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: ...
随机推荐
- sort-based shuffle的核心:org.apache.spark.util.collection.ExternalSorter
依据Spark 1.4版 在哪里会用到它 ExternalSorter是Spark的sort形式的shuffle实现的关键.SortShuffleWriter使用它,把RDD分区中的数据写入文件. o ...
- IOS 录像软件
http://iphone.91.com/tutorial/cjjc/140430/21683219.html
- 重新学struct,边界对齐,声明……与Union的区别
在内存中,编译器按照成员列表顺序分别为每个结构体变量成员分配内存,当存储过程中需要满足边界对齐的要求时,编译器会在成员之间留下额外的内存空间. 如果想确认结构体占多少存储空间,则使用关键字sizeof ...
- FZU-1926+KMP
题意:给定一篇文章和一些句子.询问句子是否在文章中出现. kmp模板题 /* kmp */ #include<stdio.h> #include<string.h> #incl ...
- linux xxd 命令
http://www.cnblogs.com/openix/archive/2012/04/23/2466320.html xxd -i dht.jpg dht.h
- 你不知道的pogo pin连接器
pogo pin连接器是一种带弹簧的探针式连接器,pogo pin连接器结构看起来非常简单,但其制造工艺要求极其的精细与复杂,从车床加工,电镀,组装等每道工序,如果没有一个有良好品质控制和完善的制造水 ...
- SQL 2008 R2 启动失败 提示 请求失败或服务未及时响应
为什么启动sql server 配置管理器出现请求失败或服务未及时响应_百度知道 http://zhidao.baidu.com/link?url=ElemzIan6I2CqJsd7-7uk5TV25 ...
- POJ3260——The Fewest Coins(多重背包+完全背包)
The Fewest Coins DescriptionFarmer John has gone to town to buy some farm supplies. Being a very eff ...
- R语言学习笔记:生成序列(Genenrating Sequences)
R提供了多种生成不同类型序列的方法.如: > x<-1:20 > x [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1 ...
- 获取html上元素的真正坐标
使用HTML元素的style.left,style.top,style.width,style.height以及width,height属性,都不能获得元素的真正位置与大小,这些属性取出来的都是原来的 ...