[LeetCode] 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.
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:14is not ugly since it includes another prime factor7.
Note:
1is 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 ...
随机推荐
- 节省Json流量
今天在实验当中发现了很不错的节省json流量方式,来做个笔记给大家分享一下. 如果跟服务器传递键值对的数组,我们一般会采用下面方式 创建一个字段 public class kv { public st ...
- 初识C#接口
C# 接口(Interface) 接口定义了所有类继承接口时应遵循的语法合同.接口定义了语法合同 "是什么" 部分,派生类定义了语法合同 "怎么做" 部分. 接 ...
- JDBC_part3_批处理_事务_元数据
本文为博主辛苦总结,希望自己以后返回来看的时候理解更深刻,也希望可以起到帮助初学者的作用. 转载请注明 出自 : luogg的博客园 谢谢配合! JDBC_day03 String a = " ...
- openresty 前端开发序
还记得第一次尝试前后端分离的时候,是使用nginx + react 构建的spa应用,后端是java,主要处理业务逻辑逻辑部分,返回json数据,在nginx里面配置好html + js纯静态文件,再 ...
- 项目编码规范(Ali)
一.研发流程规范 二.SQL编码规范 数据库命名规范:数据库名一律小写,必须以字母开头.库名包含多个单词的,以下划线“_”分隔.如果采用分库方案,分库编号从“0”开始,用“0”左补齐为四位. 表名规范 ...
- Mybatis框架的多对一关联关系(六)
一.一对多的关联映射 一对多关联查询多表数据 1接口 public interface IDeptDAO { //根据部门编号查询该部门单个查询 public Emp getEmpById(Integ ...
- Android调用webservice的例子
1.需要一个ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar的架包. 2.需要知道webservice的命名空间 // WSDL文档中的命 ...
- iOS 10对隐私权限的管理(必须要改否则会crash)
比如访问的摄像头.麦克风等硬件,都需要提前请求应用权限.允许后才可以使用,或者现在要提前声明,虽然以往要求不严格.比如在iOS10中访问通讯录时,强制必须在Info.plist中加入NSContact ...
- text-overflow
text-overflow:clip | ellipsis 默认值:clip 适用于:所有元素 clip: 当对象内文本溢出时不显示省略标记(...),而是将溢出的部分裁切掉. ellipsis: 当 ...
- Debain下解决sublime无法输入中文
sublime安装的方法在此不做过多介绍,网上有很多中教程的方式.本文描述在已经安装sublime的前提下如何输入中文. 1.保存下面的代码到文件sublime_imfix.c(位于~目录) #inc ...