-
-
Save awaemmanuel/c16a2ecd4de0358c86d26e41bd611c6d to your computer and use it in GitHub Desktop.
LeetCode - Ugly Number
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
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; | |
} | |
// | |
public boolean isUgly(int num) { | |
if(num <= 0) return false; | |
int[] factor = {2, 3 ,5}; | |
int i = 0; | |
while(num != 1){ | |
int remainder = num % factor[i]; | |
if(remainder == 0) num = num / factor[i]; | |
else if(i < factor.length-1) i++; | |
else if(i == factor.length-1) break; | |
} | |
return num == 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment