[LintCode] 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.
Notice
Note that 1 is typically treated as an ugly number.
Example
Given num = 8 return true
Given num = 14 return false
LeetCode上的原题,请参见我之前的博客Ugly Number。
解法一:
class Solution {
public:
/**
* @param num an integer
* @return true if num is an ugly number or false
*/
bool isUgly(int num) {
while (num > ) {
if (num % == ) num /= ;
else if (num % == ) num /= ;
else if (num % == ) num /= ;
else return false;
}
return num == ;
}
};
解法二:
class Solution {
public:
/**
* @param num an integer
* @return true if num is an ugly number or false
*/
bool isUgly(int num) {
if (num <= ) return false;
while (num % == ) num /= ;
while (num % == ) num /= ;
while (num % == ) num /= ;
return num == ;
}
};
[LintCode] Ugly Number 丑陋数的更多相关文章
- [LeetCode] Ugly Number 丑陋数
Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...
- [LeetCode] 263. Ugly Number 丑陋数
Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...
- Ugly number丑数2,超级丑数
[抄题]: [思维问题]: [一句话思路]:Long.valueOf(2)转换为long型再做 [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入): [画图 ...
- lintcode:Ugly Number I
Ugly Number Write a program to check whether a given number is an ugly number`. Ugly numbers are pos ...
- [LintCode] Happy Number 快乐数
Write an algorithm to determine if a number is happy. A happy number is a number defined by the foll ...
- 263 Ugly Number 丑数
编写程序判断给定的数是否为丑数.丑数就是只包含质因子 2, 3, 5 的正整数.例如, 6, 8 是丑数,而 14 不是,因为它包含了另外一个质因子 7.注意: 1 也可以被当做丑数. 输 ...
- [LeetCode] Super Ugly Number 超级丑陋数
Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...
- [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] 313. Super Ugly Number 超级丑陋数
Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...
随机推荐
- 在windows系统的文件右键菜单中增加“命令提示符”
本实用小工具能够在windows系统的文件右键菜单中增加“命令提示符”,方便快速进入制定文件的命令提示窗口,避免逐层输入或复制文件夹路径,极其实用. 工具下载地址如下:360云盘(访问密码:5b71) ...
- 偷师--先留着。。ssh,nginx,防火墙相关命令
- this和super
1.this * 每个类的每个非静态方法(没有被static修饰)都会隐含一个this引用名称,它指向调用这个方法的对象. * 当在方法中使用本类的属性时,都会隐含地使用this名称,当然也可以明确指 ...
- Laravel错误与日志处理
App\Exceptions\Handler class is where all exceptions triggered by your application are logged and th ...
- AngularJS 学习之表格
1.ng-repeat指令可以完美的显示表格: 2.在表格中显示数据: <div ng-app="myApp" ng-controller=""custo ...
- [自动运维]weblogic自动发布
近期一个项目属于测试过渡期,bug修复比较频繁,每次都会伴随着项目的打包.上传.发布,此类重复操作近乎每天都会进行,刚好最近在看python相关资料,决定重新将此前学习的weblogic的wlst相关 ...
- android访问网络--okhttp
import com.squareup.okhttp.MediaType;import com.squareup.okhttp.OkHttpClient;import com.squareup.okh ...
- 用触发器来实现Oracle的自增长列
1, 添加id列 -- ############################################### -- add ID column for XXXXXXTABLE -- #### ...
- 找规律 Codeforces Round #290 (Div. 2) A. Fox And Snake
题目传送门 /* 水题 找规律输出 */ #include <cstdio> #include <iostream> #include <cstring> #inc ...
- header元素
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...