Skip to content

Instantly share code, notes, and snippets.

@qingl97
Created July 6, 2016 11:14
Show Gist options
  • Save qingl97/7bbadee51d8e57b731e95328fb2e1900 to your computer and use it in GitHub Desktop.
Save qingl97/7bbadee51d8e57b731e95328fb2e1900 to your computer and use it in GitHub Desktop.
public class Solution {
public boolean isPowerOfThree(int n) {
// special case
if(n == 0) return false;
while(n != 1) {
// check if n can be divided by 3, return false if not
if(n%3 != 0) return false;
// take the result as input of next check until it's 1 which means n is power of 3
n = n/3;
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment