Fork me on GitHub

326.3的幂


这道题题意不难 但是不用循环或者递归的话 还是有点难度的

class Solution {
    public boolean isPowerOfThree(int n) {
        if(n == 0) return false;
		   int x = (int) (Math.log10(n)/Math.log10(3));
		   return Math.pow(3,x)==n;
	    }
}

第二种比较有意思
如果只有一个1 其他都是0 说明是3的幂 三进制

public class Solution {
    public boolean isPowerOfThree(int n) {
        return Integer.toString(n, 3).matches("^10*$");
    }
}
posted @ 2019-07-08 12:03  cznczai  阅读(95)  评论(0编辑  收藏  举报