Easy-题目19:263. Ugly Number

原创 2016年05月30日 20:03:00

题目原文:
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.
题目大意:
判断一个数是不是丑陋数。
丑陋数的定义:丑陋数是因子中只包含于2,3,5的正整数。特殊地,1是丑陋数。
题目分析:
直观地,用2,3,5分别试除这个数,若可以整除直至得到1,则该数是丑陋数。
源码:(language:java)

public class Solution {
    public boolean isUgly(int num) {
        if(num==0)
            return false;

        while(num!=1)   {
            if(num%2==0)
                num/=2;
            else if(num%3==0)
                num/=3;
            else if(num%5==0)
                num/=5;
            else
                return false;
        }

        return true;
    }
}

成绩:
2ms,beats 18.00%,众数2ms,82.00%
Cmershen的碎碎念:
此题显然有递归解法,因为n是丑数等价于n/2,n/3,n/5中有一个是丑数。但是提交递归解法会超时。

版权声明:完整版Leetcode题解请出门左转https://github.com/cmershen1/leetcode/tree/master/docs

相关文章推荐

263. Ugly Number [easy] (Python)

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

LeetCode 263 Ugly Number(丑数)

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

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

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

20170307-leetcode-263-Ugly Number

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

[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 ...

LeetCode 202. Happy Number & 263. Ugly Number

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

LeetCode第264题目 Ugly Number ll

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

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...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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