【leetcode】Power of Three


判断一个数是否是3的倍数。


题目有很多淫巧,参考 :

 http://www.cnblogs.com/grandyang/p/5138212.html


A summary of `all` solutions


-6 % 3 = 0,

-1 % 3 = -1,

-2 % 3 = -2.


public class Solution {
    public boolean isPowerOfThree(int n) {
        if (n == 0)
	    return false;
		
        while(n % 3 == 0 ){
        	n /= 3;
        }
	return n == 1;
    }
}


你可能感兴趣的:(【leetcode】Power of Three)