[LeetCode] Ugly Number
2015-08-19 18:59
375 查看
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, 8are ugly while
14is
not ugly since it includes another prime factor
7.
Note that
1is typically treated as an ugly number.
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
解题思路:
题意为只有2、3、5三个因子的数成为Ugly Number。1也认为是Ugly Number。因此我们只需要不断的除以2/3/5即可(倘若能够整除)
class Solution {
public:
bool isUgly(int num) {
if(num<=0){
return false;
}
while(num%2==0){
num /= 2;
}
while(num%3==0){
num /= 3;
}
while(num%5==0){
num /= 5;
}
return num==1;
}
};
相关文章推荐
- ios之 微信扫码登录网页实现原理
- udp tcp fax发送
- 题目:判断字符串是否没有重复字符
- GET-POST请求,数据的持久化
- mysql information_schema
- Bitmap基本概念及在Android4.4系统上使用BitmapFactory的注意事项
- 生活感悟
- 题目:删除链表中倒数第n个节点
- 图像处理
- Parallels Desktop如何创建虚拟机模板
- 题目:删除排序链表中的重复元素
- main() 参数:argc 与 argv
- Android笔记(三)为按钮添加事件
- linux下类似Bus Hound的工具
- 字符串常用函数汇总【持续更新中........】
- [LightOJ1370]Bi-shoe and Phi-shoe
- 题目:删除排序数组中的重复数字 II
- Linux 命令行Shell快捷键
- HDU-1875 畅通工程再续(最小生成树[Prim])
- 暗色调,调出紫红色的暗调晨曦照片效果(转)