Ugly Number,Ugly Number II,Super Ugly Number
一.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;
while(num%==){
num /= ;
}
while(num%==){
num /= ;
}
while(num%==){
num /= ;
}
return num== ? true:false;
}
};
二.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.
class Solution {
public:
int nthUglyNumber(int n) {
int index2=,index3=,index5=;
int ugly2=,ugly3=,ugly5=;
vector<int> res(n,);
int index = ;
while(index < n){
while(res[index2]* <= res[index-]){
index2++;
}
while(res[index3]* <= res[index-]){
index3++;
}
while(res[index5]* <= res[index-]){
index5++;
}
ugly2 = res[index2]*;
ugly3 = res[index3]*;
ugly5 = res[index5]*;
res[index++] = min(ugly2,min(ugly3,ugly5));
}
return res[n-];
}
};
Super Ugly Number
Write a program to find the nth super ugly number.
Super ugly numbers are positive numbers whose all prime factors are in the given prime list primes
of size k
. For example, [1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32]
is the sequence of the first 12 super ugly numbers given primes
= [2, 7, 13, 19]
of size 4.
Note:
(1) 1
is a super ugly number for any given primes
.
(2) The given numbers in primes
are in ascending order.
(3) 0 < k
≤ 100, 0 < n
≤ 106, 0 < primes[i]
< 1000.
class Solution {
public:
int nthSuperUglyNumber(int n, vector<int>& primes) {
int primes_size = primes.size();
vector<int> vec_indexs(primes_size,) ,vec_uglys(n,) ;
for(int i=;i<n;++i){
int min_ugly = INT_MAX;
for(int j=;j<primes_size;++j){
int index = vec_indexs[j];
while(primes[j]*vec_uglys[index] <= vec_uglys[i-]){
index++;
}
min_ugly = min(min_ugly,primes[j]*vec_uglys[index]);
vec_indexs[j] = index;
}
vec_uglys[i] = min_ugly;
}
return vec_uglys.back();
}
};
Ugly Number,Ugly Number II,Super Ugly Number的更多相关文章
- 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] Super Ugly Number 超级丑陋数
Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...
- Leetcode 313. super ugly number
Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...
- 313. Super Ugly Number
题目: Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose ...
- Super Ugly Number -- LeetCode
Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...
- [LeetCode] 313. Super Ugly Number 超级丑陋数
Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...
- [LintCode] Super Ugly Number 超级丑陋数
Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...
- [LeetCode] Super Ugly Number (Medium)
Super Ugly Number 最后WA没做出来. typedef long long int64; #define MAXBOUND 10000000 class Solution { publ ...
- [Swift]LeetCode313. 超级丑数 | Super Ugly Number
Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...
随机推荐
- 简单随笔——如何在gridview的页脚显示信息。。。。
我是超级大菜鸟...哈哈 先上图看看是不是你需要的 第一步,右击gridview,在事件中,单击RowdataBond事件. 在这之前一定要记得在gridview属性中的ShowFooter设置为“t ...
- js获取昨天日期
刚刚js做项目,遇到需要获取昨天日期的问题,网上找了下答案,感觉网上的答案都不太严谨,自己写了个,凑合能用吧,忘大神们抛砖指教. <script type="text/javascri ...
- WebService调用1(.Net)
1.创建一个最简单的Web Service (1) 新建-项目-ASP.NET空WEB应用程序 (2)添加新项-WEB服务 默认会添加一个HelloWorld方法: using System; us ...
- go JSON
package utils import ( "encoding/json" "errors" ) func JsonToMap(text []byte) (m ...
- oracle 之 COMMENT
http://blog.csdn.net/liguihan88/article/details/3002403 无疑注释现在都被大家接受和认可,在大家编程用的IDE中都提供或有第三方插件来支持提取注释 ...
- wampserver 自定义站点
wampserver配置多站点,安装完wampserver后,我们要做的肯定是很多项目,那么如何配置wampserver多站点呢. 在“httpd.conf”文件中查找:Include conf/ex ...
- python GUI学习——Tkinter
支持python的常见GUI工具包: Tkinter 使用Tk平台 很容易得到 半标准 wxpython 基于wxWindows.跨平台越来越流行 Python Win 只能在Windows上使用 使 ...
- python列表推导和字典推导
代码如下: list = ['aaa','bbb','ccc','ddd'] dict = {key:value for value,key in enumerate(list)} print(dic ...
- Leetcode 171 Excel Sheet Column Number python
题目: Given a column title as appear in an Excel sheet, return its corresponding column number. For ex ...
- Linux下 nginx + 最新版php5.5 安装配置详解
1.nginx的安装: 首先nginx的安装需要依赖最基础的三个包,这里面我们不设计更多的扩展模块,只是安装最基础的三个包, zlib 此包主要是对http内容进行gzip压缩,减少网络传输流量 PC ...