[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 ...
随机推荐
- ASP.NET 4.0 取消表单危险字符验证
/// <summary> /// ASP.NET4.0 表单验证类 /// </summary> public class FormRequestValidation : R ...
- Codeforces Round#250 D. The Child and Zoo(并差集)
题目链接:http://codeforces.com/problemset/problem/437/D 思路:并差集应用,先对所有的边从大到小排序,然后枚举边的时候,如果某条边的两个顶点不在同一个集合 ...
- 【T_SQL】 基础 续+++
十五.T-SQL 编程 1.变量 (1)局部变量 A.局部变量必须以标记@作为前缀 ,如@age. B.局部变量的使用也是先 ...
- Junit的简单使用
Junit是一个很好用的单元测试工具,下面是使用Junit来测试方法的简单案例: import java.util.ArrayList; import java.util.Iterator; impo ...
- LR连接oracle时出现:SQLState=28000[Oracle][ODBC][Ora]ORA-01017:invalid username/password;logon denied
出现的现象:
- JavaScript 之 iframe自适应问题---可以用来实现网页局部刷新
1.HTML <iframe src="index.html" id="iframepage" frameborder="0" scr ...
- 仓库如何盘点 打印扫描一体PDA盘点机提升库存盘点效率
仓库盘点是对仓储货品的收发结存等活动进行有效控制,保证仓储货品完好无损.帐物相符,确保生产正常进行,规范公司物料的盘点作业.盘点需人工操作,费时费力,PDA盘点机的出现大幅提升了盘点效率,减轻了工作人 ...
- 移动网页 -- CSS布局
1.多栏结构 column-count column-width column:120px 3: column-gap:2em: column-rule:2px dotted gray: 跨越以及打断 ...
- 转:delphi异常捕获try except语句 和 try finally语句用法
转:http://www.java123.net/v/936977.html 2015-06-24 09:27:48 一直写程序都没管他们,也尽量很少用,今天终于想把他给弄个明白,在网上找来 ...
- 操作TAB文件和TStringGrid赋值;
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...