Skip to content

Instantly share code, notes, and snippets.

@cangoal
Last active April 14, 2016 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cangoal/56b971ee201eac051a5a to your computer and use it in GitHub Desktop.
Save cangoal/56b971ee201eac051a5a to your computer and use it in GitHub Desktop.
LeetCode - Ugly Number
//
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