[LeetCode] Ugly Number (A New Question Added Today)
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
.
Note that 1
is typically treated as an ugly number.
这是今天才加上去的新题。没啥可说的。只要把ugly number的定义搞清楚就可以很好的写了。
因为prime factor只能为2,3,5。所以只要一个数不能被分解为只含有这三个数的乘式那么它就不是一个ugly number了。
代码如下。~
public class Solution {
public boolean isUgly(int num) {
if(num<=0){
return false;
}
if(num==1){
return true;
}
while(num!=1){
if(num%2==0){
num=num/2;
}else if(num%3==0){
num=num/3;
}else if(num%5==0){
num=num/5;
}else{
return false;
}
}
return true;
}
}
[LeetCode] Ugly Number (A New Question Added Today)的更多相关文章
- 力不从心 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 (A New Question Added Today)
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- [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 丑陋数
Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...
- [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 ...
- LeetCode——Ugly Number
Description: Write a program to check whether a given number is an ugly number. Ugly numbers are pos ...
- LeetCode() Ugly Number II 背下来!
一个别人,非常牛逼的思路,膜拜了!orz!!!! vector <int> results (1,1); int i = 0, j = 0, k = 0; while (results.s ...
- LeetCode Ugly Number (简单题)
题意: 判断是一个数的质因子仅含有2,3,5这3个. 思路: 因子2比较容易解决,num/=num-(num&num-1)就可以了.3和5的需要通过循环来另判. C++ class Solut ...
随机推荐
- POJ2891——Strange Way to Express Integers(模线性方程组)
Strange Way to Express Integers DescriptionElina is reading a book written by Rujia Liu, which intro ...
- 用maven进行测试
maven的重要职责之一就是自动运行单元测试,它通过maven-surefire-plugin与主流的单元测试框架junit和testng集成,并且能够自动生成丰富的结果报表. maven并不是一个单 ...
- 23.allegro中自动布线[原创]
1. --- 方法①:选择网络自动布线 -- --- 已经步好: --- 方法②: ---- ---- 布线: --- 方法③: -- ----
- bzoj2242: [SDOI2011]计算器 && BSGS 算法
BSGS算法 给定y.z.p,计算满足yx mod p=z的最小非负整数x.p为质数(没法写数学公式,以下内容用心去感受吧) 设 x = i*m + j. 则 y^(j)≡z∗y^(-i*m)) (m ...
- UVa 11526 H(n)
题意: long long H(int n){ long long res = 0; for( int i = 1; i <= n; i=i+1 ){ res = (res + n/i); } ...
- cocos2d-x之 CCSpriteBatchNode 用法总结
例子1: CCSpriteBatchNode* batch = [CCSpriteBatchNode batchNodeWithFile:@"table.png"]; ...
- How to modify squashfs image
/********************************************************************** * How to modify squashfs ima ...
- erl0001-Erlang 设计原则 process port io
Erlang原理 (转载自ITEYE cryolite博客 ps:精彩)by Robert Virding This is a description of some of the basic pro ...
- LeetCode: Interval
(1)Merge Intervals https://leetcode.com/problems/merge-intervals/ Given a collection of intervals, m ...
- DirectShow建立一个视频捕捉程序
DirectShow 提供了用应用程序从适当的硬件中捕捉和预览音/视频的能力.数据源包括:VCR,camera,TV tuner,microphone,或其他的数据源.一个应用程序可以立刻显示捕捉的数 ...