[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:
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 ...
随机推荐
- Unity3d连接SQL Server数据库出现SocketException: 使用了与请求的协议不兼容的地址错误
这两天,同学问我Unity3d连接SQL Server的问题,当时我只是简单的说:“应该一样吧,就是那简单的几句啊”.之后他让我试了下,我才发现有问题了.故此写下一篇博客,要牢记这件事的教训,操作数据 ...
- [占位-未完成]scikit-learn一般实例之十:核岭回归和SVR的比较
[占位-未完成]scikit-learn一般实例之十:核岭回归和SVR的比较
- 微信企业号开发(1)WebAPI在回调模式中的URL验证
微信回调模式的官方文档. 开发语言:C#(微信相关功能代码可以从官网下载) 首先,必须要明确几个参数,这几个参数在微信企业号中,每次调用都会使用到. 1.msg_signature:签名(已加密,加密 ...
- JSON扩展类——JsonHelper
1.引用Newtonsoft.Json库(JSON.NET). 2.复制粘贴JsonHelper吧. 源代码: using System; using System.Collections.Gener ...
- 学习Redis你必须了解的数据结构——JS实现集合和ECMA6集合
集合类似于数组,但是集合中的元素是唯一的,没有重复值的.就像你学高中数学的概念一样,集合还可以做很多比如,并集,交集,差集的计算.在ECMA6之前,JavaScript没有提供原生的Set类,所以只能 ...
- Entity Framework Plus 系列目录
Entity Framework Plus 系列文章计划的已经全部写完,可能还有其他功能没有写到,希望大家能够多动手,尝试一下使用,一定会给您带来一些帮助的.文章全部写完,也应该出一个目录方便查看,目 ...
- python 数据类型---列表使用 之二 (增删改查)
列表的操作 1.列表的修改 >>> name ['Frank', 'Lee', 2, ['Andy', 'Troy']] >>> name[0] = "F ...
- PDO运用
- css知多少(11)——position
1. 引言 本文将用一篇文章介绍position(定位),在学习position之前,我们应该去思考一个问题:什么情况下我们需要定位?如果没有定位将无法满足我们怎样的需求?我们要知道,被人类创造出来的 ...
- 纯css3 3D图片立方体旋转动画特效
纯css3 3D立方体模块,鼠标触碰,模块炸开,大立方体中套小立方体 效果展示 手机扫描二维码体验效果: 效果图如下: 源码下载:http://hovertree.com/h/bjaf/0qmul8g ...