LeetCode 263. Ugly Number
原题链接在这里:https://leetcode.com/problems/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.
题解:
判断一个数是不是ugly number, 是看它能否被2, 3, 5整除即可.
AC Java:
1 public class Solution { 2 public boolean isUgly(int num) { 3 if(num == 0){ 4 return false; 5 } 6 while(num%2 == 0){ 7 num/=2; 8 } 9 while(num%3 == 0){ 10 num/=3; 11 } 12 while(num%5 == 0){ 13 num/=5; 14 } 15 return num == 1; 16 } 17 }