Java implementation LeetCode 263 the number of ugly

263. The number of ugly

Write a program to determine whether a given number is a number of ugly.

Ugly contains only the number of prime factors is a positive integer of 2, 3, 5.

Example 1:

Input: 6
Output: true
interpretation: 6 = 2 × 3
Example 2:

Input: 8
Output: true
interpretation: 8 = 2 × 2 × 2
Example 3:

Input: 14
Output: false
explanation: the number 14 is not ugly, because it contains another prime factor 7.
Description:

1 is the number of ugly.
Does not exceed the range of the input 32-bit signed integers: [-231 231--1].

class Solution {
     public boolean isUgly(int num) {
        if (num<1) return false;
        while (num%5==0){
            num/=5;
        }
        while (num%3==0){
            num/=3;
        }
        while (num%2==0){
            num>>=1;
        }
        return num == 1;
    }
}
Released 1388 original articles · won praise 10000 + · views 1.41 million +

Guess you like

Origin blog.csdn.net/a1439775520/article/details/104643218