Problem:

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.

Summary:

判断一个数是不是ugly number,即质因子只有2、3、5的数。

Analysis:

判断方法即为不断除以2、3、5,看最终能否得到1。以下两种方法大同小异。

 class Solution {
public:
bool isUgly(int num) {
if (num <= ) return false; while (num != ) {
if (num % == ) num /=;
else if (num % == ) num /=;
else if (num % == ) num /= ;
else return false;
} return true;
}
};
 class Solution {
public:
bool isUgly(int num) {
if (num <= ) return false; while (num >= && num % == ) num /= ;
while (num >= && num % == ) num /= ;
while (num >= && num % == ) num /= ; return num == ;
}
};

LeetCode 263 Ugly Number的更多相关文章

  1. leetcode 263. Ugly Number 、264. Ugly Number II 、313. Super Ugly Number 、204. Count Primes

    263. Ugly Number 注意:1.小于等于0都不属于丑数 2.while循环的判断不是num >= 0, 而是能被2 .3.5整除,即能被整除才去除这些数 class Solution ...

  2. LN : leetcode 263 Ugly Number

    lc 263 Ugly Number lc 263 Ugly Number Write a program to check whether a given number is an ugly num ...

  3. [LeetCode] 263. Ugly Number 丑陋数

    Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...

  4. Leetcode 263 Ugly Number 数论 类似质因数分解

    Ugly Number的质因数仅为2,3,5 将输入的数分别除以2,3,5直到不能除,看是否为1,为1的是Ugly Number,其他则不是. class Solution { public: boo ...

  5. (easy)LeetCode 263.Ugly Number

    Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...

  6. Java [Leetcode 263]Ugly Number

    题目描述: Write a program to check whether a given number is an ugly number. Ugly numbers are positive n ...

  7. [leetcode] 263. Ugly Number (easy)

    只要存在一种因数分解后,其因子是2,3,5中的一种或多种,就算是ugly数字. 思路: 以2/3/5作为除数除后,最后结果等于1的就是ugly数字 Runtime: 4 ms, faster than ...

  8. [LeetCode] 264. Ugly Number II 丑陋数 II

    Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...

  9. 263. Ugly Number(C++)

    263. Ugly Number Write a program to check whether a given number is an ugly number. Ugly numbers are ...

随机推荐

  1. AngularJS 国际化——Angular-translate

    对于一个用户群面向全球的的应用来说,不得不考虑国际化的问题.当然,即便是刚刚起步的小应用,如果有心搞大,也应该提前设计国际化的方案. 本篇讲述使用AngularJS构建的应用的简单国际化方案,准确的说 ...

  2. git 初始化

    Git global setup git config --global user.name "杨清1" git config --global user.email " ...

  3. nyoj 236心急的C小加 动态规划( java)

    sort函数用法: #include<algorithm> using namespace std; sort(a,a+len;cmp)    //a-->数组名,    len-- ...

  4. substr — 详解

    substr — 返回字符串的子串 举例说明: string substr ( string $string , int $start , int $length ) 返回字符串 string 由 s ...

  5. (一)css代码积累——自己经常忘记,但是总记不住的代码

    1.透明度设置 90%透明:filter:alpha(opacity=90);-moz-opacity:0.90;-khtml-opacity: 0.90;opacity: 0.90; 80%透明:f ...

  6. 为在韶大痛苦而不能用手机、Pad等上网的同志造福!

    目标:共享咱们校园网,让更多的人或更多的设备冲浪去! 基本条件:一台带无线功能的笔记本,一个可以上网的账号与pwd,最好为Windows7以上的操作系统,如果是XP,则需要打个.net framewo ...

  7. Gunicorn 问题

    Does Gunicorn suffer from the thundering herd problem? The thundering herd problem occurs when many ...

  8. Reflow(渲染)和Repaint(重绘)

    Reflow(渲染):对于DOM结构中的各个元素都有自己的盒模型,浏览器根据各种样式(浏览器的.开发人员定义的等)来计算,并根据计算结果将元素放到它该出现的位置,这个过程称之为reflow. refl ...

  9. unity 2d 和 NGUI layer

    http://blog.csdn.net/xtxy/article/details/37876825 在使用unity2d开发游戏的时候,使用了NGUI作为界面,本来二者配合得还挺好,但是一个使用场景 ...

  10. UIMenuController使用

    - (void)bubbleDidLongPress:(UILongPressGestureRecognizer *)gestureRecognizer { if(gestureRecognizer. ...