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
.
Example 1:
Input: 6
Output: true
Explanation: 6 = 2 × 3
Example 2:
Input: 8
Output: true
Explanation: 8 = 2 × 2 × 2
Example 3:
Input: 14
Output: false
Explanation:14
is not ugly since it includes another prime factor7
.
Note:
-
1
is typically treated as an ugly number. - Input is within the 32-bit signed integer range: [−231, 231 − 1].
这道题让我们检测一个数是否为丑陋数,所谓丑陋数就是其质数因子只能是 2,3,5。那么最直接的办法就是不停的除以这些质数,如果剩余的数字是1的话就是丑陋数了,参见代码如下:
解法一:
class Solution {
public:
bool isUgly(int num) {
while (num >= ) {
if (num % == ) num /= ;
else if (num % == ) num /= ;
else if (num % == ) num /= ;
else return false;
}
return num == ;
}
};
我们也可以换一种写法,分别不停的除以 2,3,5,并且看最后剩下来的数字是否为1即可,参见代码如下:
解法二:
class Solution {
public:
bool isUgly(int num) {
if (num <= ) return false;
while (num % == ) num /= ;
while (num % == ) num /= ;
while (num % == ) num /= ;
return num == ;
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/263
类似题目:
参考资料:
https://leetcode.com/problems/ugly-number/
https://leetcode.com/problems/ugly-number/discuss/69225/My-2ms-java-solution
https://leetcode.com/problems/ugly-number/discuss/69214/2-4-lines-every-language
https://leetcode.com/problems/ugly-number/discuss/69332/Simple-java-solution-with-explanation
https://leetcode.com/problems/ugly-number/discuss/69308/Java-solution-greatest-divide-by-2-3-5
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Ugly Number 丑陋数的更多相关文章
-
[LeetCode] 263. Ugly Number 丑陋数
Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...
-
[LintCode] Ugly Number 丑陋数
Write a program to check whether a given number is an ugly number`. Ugly numbers are positive number ...
-
力不从心 Leetcode(ugly number heap) 263, 264,313
Leetcode ugly number set (3 now) new ugly number is generated by multiplying a prime with previous g ...
-
[LeetCode] Ugly Number II 丑陋数之二
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
-
[LeetCode] Happy Number 快乐数
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
-
[LeetCode] Ugly Number II
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
-
[LeetCode] Ugly Number
Ugly Number Total Accepted: 20760 Total Submissions: 63208 Difficulty: Easy Write a program to check ...
-
Ugly number丑数2,超级丑数
[抄题]: [思维问题]: [一句话思路]:Long.valueOf(2)转换为long型再做 [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入): [画图 ...
-
LeetCode——Ugly Number
Description: Write a program to check whether a given number is an ugly number. Ugly numbers are pos ...
随机推荐
-
Android ListView onItemClick Not Work
Android ListView onItemClick Not Work ListView item中有Button和RadioButton的时候,它的Item点击事件不起作用,需要设置item的属 ...
-
跟着百度学PHP[2]-foreach条件嵌套
任务 通过二维数组,保存了学号.姓名和成绩,可以通过两个循环嵌套,遍历出学号和姓名. <?php $student = array( '001' => array("王大牛&qu ...
-
Excel的文件打开特别慢,xls文件特别大解决一例
Excel的文件打开特别慢,xls文件特别大解决一例 打开Excel的xls文件打开特别慢,而且操作也非常慢,动辄需要10几20分钟,很不正常.一个简单的Excel的xls文件有10几兆,甚至几百兆的 ...
-
Visual Studio 2015安装过程卡住,解决办法
Visual Studio 2015安装过程中卡住的情况有很多,我遇到的是卡在安装windows 更新KB2999226,解决办法 手动安装此更新包,位置在Visual Studio的安装包中,目录: ...
-
A Round Peg in a Ground Hole(判断是否是凸包,点是否在凸包内,圆与多边形的关系)
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4628 Accepted: 1434 Description The D ...
-
php 之 PDO数据访问抽象层(0513)
PDO(PHP Data Objects)是一种在PHP里连接数据库的使用接口. PDO与mysqli曾经被建议用来取代原本PHP在用的mysql相关函数, 基于数据库使用的安全性,因为后者欠缺对于S ...
-
MYSQL 执行计划
Explain语法 EXPLAIN SELECT …… 变体: 1. EXPLAIN EXTENDED SELECT …… 将执行计划“反编译”成SELECT语句,运行SHOW WARNINGS 可得 ...
-
spring4.1.3+springmvc+mybatis3.2.1整合
注意:这里使用了mybatis3.2.1版本,刚开始用了3.4.1的版本,会报一个很奇怪的错(java.lang.AbstractMethodError: org.mybatis.spring.tra ...
-
[HEOI2015]小Z的房间(矩阵树定理学习笔记)
题目描述 你突然有了一个大房子,房子里面有一些房间.事实上,你的房子可以看做是一个包含n*m个格子的格状矩形,每个格子是一个房间或者是一个柱子.在一开始的时候,相邻的格子之间都有墙隔着. 你想要打通一 ...
-
.NET Core 2.0 Cookie中间件 权限验证
:在ConfigureServices添加Cookie中间件,使用自定义Scheme services.AddAuthentication(options=> { options.Default ...