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

Total Accepted: 2860 Total Submissions: 9791 Difficulty: Medium

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的更多相关文章

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

  2. [LeetCode] Super Ugly Number 超级丑陋数

    Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...

  3. Leetcode 313. super ugly number

    Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...

  4. 313. Super Ugly Number

    题目: Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose ...

  5. Super Ugly Number -- LeetCode

    Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...

  6. [LeetCode] 313. Super Ugly Number 超级丑陋数

    Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...

  7. [LintCode] Super Ugly Number 超级丑陋数

    Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...

  8. [LeetCode] Super Ugly Number (Medium)

    Super Ugly Number 最后WA没做出来. typedef long long int64; #define MAXBOUND 10000000 class Solution { publ ...

  9. [Swift]LeetCode313. 超级丑数 | Super Ugly Number

    Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...

随机推荐

  1. ASP.NET Signalr 2.0 实现一个简单的聊天室

    学习了一下SignalR 2.0,http://www.asp.net/signalr 文章写的很详细,如果头疼英文,还可以机翻成中文,虽然不是很准确,大概还是容易看明白. 理论要结合实践,自己动手做 ...

  2. 创建GIF loading图片

    第一步 新建一个宽80PX 高10PX的文档 第二步 做8个宽8PX的方格 黄色色值#e7a521 红色色值#ff0000(可根据自己的喜好设定) 第三步 复制7个层(共8个图层)每个图层相应改变红色 ...

  3. ASP.NET属性之AssociatedControlID

    AssociatedControlID 是用在Asp.Net 中的 Label 控件上.给label控件关联一个ASP.NET的控件,在点击 这个 LABEL的时候,所关联的ASP.NET控件会获得焦 ...

  4. 在配置WCF服务的时候出现的错误总结

    1.由于扩展配置问题而无法提供您请求的页面.如果该页面是脚本,请添加处理程序.如果应下载文件,请添加 MIME 映射. 我是通过安装图中的FramWork3.5.1搞定的. 网上的其他参考: http ...

  5. ComboGrid 行内点击编辑内容

    最近easyui需要在行内编辑选中项,但是编辑的内容出了当前选中列值,还有其他的,比较麻烦, 先看下这段代码 columns: [[ { field: 'GuestID', title: '编号', ...

  6. [转]activiti5用户任务分配

    用户任务分配办理人:1.用户任务可以直接分配给一个用户,这可以通过humanPerformer元素定义. humanPerformer定义需要一个 resourceAssignmentExpressi ...

  7. MYSQL5.6数据库ZIP安装以及VS中使用注意事项

    先挂资源,下载地址: http://download.softagency.net/MySQL/Downloads/. 找到MYSQL5.6,两个版本看系统而定我下的是64位的:http://down ...

  8. c语言函数注意点

    返回值的基本概念.return的作用    return 1>void可以省略return 2>可以多次使用return 3>return后面不能有其他语句   4. 函数的弱语法 ...

  9. SQL Server 对表的 12 种一般性操作

    01. 创建 create table Strings(ID int);    go 02. 为表添加列 alter table Strings    add String nvarchar(32); ...

  10. 混合使用Azure LB和ILB访问相同web服务(1)

    在经典的使用场景中,我们一般使用AzureLoadBalancer来面向公网提供负载均衡服务,而使用Azure Internal Load Balancer提供内部那些不愿意将服务暴露给公网的服务,比 ...