[leetcode] 263. Ugly Number

原创 2016年06月02日 11:26:20

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.

Solution:

class Solution {
public:
    bool isUgly(int num) {
        if (num<=0) return false;
        
        while(num%2==0 || num%3==0 || num%5==0){
            if (num%2==0)
                num = num/2;
            else if(num%3==0)
                num = num/3;
            else if(num%5==0)
                num = num/5;
        }
        return num ==1;
        
    }
};


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

LeetCode 263 Ugly Number(丑数)

翻译写一个程序来检查给定的数字是否是丑数(Ugly number)。丑数是一个正数,它的质因子只包括2、3、5。例如,6、8是丑数,而因为包含了7这个因子,所以14不是丑数。请注意,1通常被视为一个丑...
  • NoMasp
  • NoMasp
  • 2016年01月18日 13:24
  • 1883

剑指offer 面试题34:寻找丑数(Leetcode 263.ugly number)解题报告

剑指offer 面试题34:寻找丑数 题目:把只包含因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含因子7。习惯上我们把1当做是第一个丑数。请按从小到大...

[leetcode]263. Ugly Number

题目 Write a program to check whether a given number is an ugly number. Ugly numbers are posit...

Leetcode 263-264 ugly number

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

20170307-leetcode-263-Ugly Number

1.DescriptionWrite a program to check whether a given number is an ugly number.Ugly numbers are posi...

LeetCode 202. Happy Number & 263. Ugly Number

202. Happy Number Write an algorithm to determine if a number is "happy". A happy number is ...

leetcode263_264:Ugly Number & Ugly Number II

Ugly NumberWrite a program to check whether a given number is an ugly number.Ugly numbers are positi...

Leetcode 263. Ugly Number & 264. Ugly Number II

263. Ugly Number Total Accepted: 58196 Total Submissions: 157221 Difficulty: Easy Write a pro...

263. Ugly Number [easy] (Python)

题目链接 https://leetcode.com/problems/ugly-number/ 题目原文 Write a program to check whether a given num...

LeetCode Ugly Number i,ii详解

// Date : 2016.07.25 // Author : yqtao // https://github.com/yqtaowhu/**************************...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:[leetcode] 263. Ugly Number
举报原因:
原因补充:

(最多只允许输入30个字)