263. Ugly Number 丑数
2016-06-25 09:43
387 查看
https://leetcode.com/problems/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
Note that
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.
class Solution { public: bool isUgly(int num) { if(num<=0){ return false; } if(num==1){ return true; } if(num%2==0){ return isUgly(num/2); } if(num%3==0){ return isUgly(num/3); } if(num%5==0){ return isUgly(num/5); } return false; } };
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?true:false; } };
相关文章推荐
- Firefox清空缓存的快捷键
- 想赚大钱,就别太着急!
- textField:shouldChangeCharactersInRange:replacementString:
- [随记]浅谈pdfobject.js实现网页PDF文件浏览
- JSP简单运行原理----------九大隐式对象和四大域对象
- iOS多线程编程
- VS2013/VS2015无法安装Visual Assist 调试笔记
- poj 1144 Network 无向图求割点
- AbAndroidStudio报错“unable to create debug bridge:ubable to start adb server”
- leetcode 33. Search in Rotated Sorted Array
- java下double相乘精度丢失问题
- Linux下文件的压缩和解压
- 每天一个Linux命令(11)--nl命令
- Java读取Level-1行情dbf文件极致优化(2)
- Android之zxing二维码生成与识别
- shell脚本实现动态时钟
- 王通:未来品牌的三重境界
- null和""的区别
- iOS开发 微信SDK的分享功能
- 嵌入式linux开发uboot移植(五)——uboot命令体系