Tc的专栏

待重头,收拾旧山河,朝天阙

LeetCode 263 Ugly Number

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.


题目链接:https://leetcode.com/problems/ugly-number/

题目分析:除除就好

public class Solution {
    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;
        }
        return num == 1;
    }
}


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Tc_To_Top/article/details/52384092
文章标签: leetcode
个人分类: Leetcode Easy
所属专栏: LeetCode
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭