LeetCode:Ugly Number - 丑数1:判断指定数字是否为丑数

一、题目名称java

Ugly Number(丑数1:判断指定数字是否为丑数).net

二、题目地址code

https://leetcode.com/problems/ugly-numberblog

三、题目内容leetcode

英文:Write a program to check whether a given number is an ugly number.开发

中文:写程序判断指定数字是否为丑数get

说明:丑数具备以下特征:1是丑数,丑数能够表示为有限个二、三、5的乘积it

注意:本题的目标不是“判断第N个丑数”,关于“判断第N个丑数”,请参考Ugly Number II(LeetCode #264)io

四、解题方法class

根据丑数的定义,能够经过以下方法判断一个数字n是否为丑数:能够试着用二、三、5不断整除n,当n不能再被二、三、5整除时,判断n是否等于1,等于1则指定的数字是丑数(返回真),不然不是(返回假)。

Java代码以下:

/**
 * 功能说明:LeetCode 263 - Ugly Number
 * 开发人员:Tsybius2014
 * 开发时间:2015年8月23日
 */
public class Solution {
    
    /**
     * 判断数字是否为丑数
     * @param num 被判断数字
     * @return true:丑数,false:非丑数
     */
    public boolean isUgly(int num) {
        
        if (num <= 0) {
            return false;
        }
        
        while (num % 2 == 0) num /= 2;
        while (num % 3 == 0) num /= 3;
        while (num % 5 == 0) num /= 5;
        
        if (num == 1) {
            return true;
        } else {
            return false;
        }
    }
}

END

相关文章
相关标签/搜索