Leetcode #263 Ugly Number
2015-09-11 01:15
316 查看
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
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
Difficulty: Easy
Python:
c++:
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.
Difficulty: Easy
Python:
class Solution(object): def isUgly(self, num): """ :type num: int :rtype: bool """ if num==0: return False while num%2==0: num = num/2 while num%3==0: num = num/3 while num%5==0: num = num/5 if num ==1: return True return False
c++:
class Solution { public: bool isUgly(int num) { if(num==0) return false; if(num==1) return true; while(num%2==0) { num = num/2; } while(num%3==0) { num = num/3; } while(num%5==0) { num = num/5; } if(num==1) return true; return false; } };
相关文章推荐
- axure笔记
- apt-get安装软件Unable to locate package错误解决办法
- hdu 3016(线段树+dp)
- 简历上的公关战
- IOS中Json解析的四种方法
- 常用js代码
- Magento 默认排序为最新产品在前 How to sort Magento products by date added as default
- axture实训2
- java实现 分类汇总
- 2015.09.10 hihoCoder(C++)
- Leetcode #279 Perfect Squares
- VC6.0 和VS2008 加载IPHlpApi.LIB及IPHlpApi.H 失败 方法
- Leetcode Reverse Linked List
- 1094. The Largest Generation (25)
- P2P之UDP穿透NAT的原理与实现
- 移动客户端开发的一点小小的思考
- atlas tutorial
- 剑指Offer面试题:27.最小的k个数
- MySQL for mac如何调出命令行客户端MySQL Command Line Client
- CentOS6.5下kafka+ZooKeeper下载与安装详细教程-多机版本2-附录