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.
本题要求求出ugly number,不是很难,但是要注意考虑边界条件,比如1,0两种情况,代码如下:
1 public class Solution { 2 public boolean isUgly(int num) { 3 int[] factor = new int[]{2,3,5}; 4 if(num==0) return false; 5 for(int i=0;i<factor.length;i++){ 6 while(num%factor[i]==0){ 7 num/=factor[i]; 8 } 9 } 10 return num==1; 11 } 12 }