LeetCode:Ugly Number
2015-10-26 15:09
281 查看
问题描述:
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; } while(num) { if(num % 2 == 0) { num /= 2; } else if(num % 3 == 0) { num /= 3; } else if(num % 5 == 0) { num /= 5; } else { return false; } if(num == 1) { return true; } } } };
相关文章推荐
- R in Action 学习笔记 - 第九章-Analysis of Variance
- 离线谷歌卫星地图加载服务中间件调用示例 For WPF
- 两个较为流行的团队发展模式
- 音视频软件助力智能门禁技术发展
- solr 新建core
- Android编程中TextView宽度过大导致Drawable无法居中问题解决方法
- JAVA事务
- ASp.Net Websocket 是什么(一)
- android studio 断点调试
- Garbage Collection Optimization for High-Throughput and Low-Latency Java Applications--转载
- Javascript use strict模式和对象
- CI整合Smarty
- (NO.00001)iOS游戏SpeedBoy Lite成形记(二十一)
- (NO.00001)iOS游戏SpeedBoy Lite成形记(二十一)
- (NO.00001)iOS游戏SpeedBoy Lite成形记(二十一)
- spring整合hibernate(注解、xml)applicationContext.xml配置
- 四种强制类型转换的总结
- IOS--JSON解析之JSONKit使用
- yarn关于app max attempt深度解析,针对长服务appmaster平滑重启
- android源码 键盘消息处理机制