Created
July 6, 2016 11:14
-
-
Save qingl97/7bbadee51d8e57b731e95328fb2e1900 to your computer and use it in GitHub Desktop.
This file contains 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 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