*LeetCode-Power of Two

来源:互联网 发布:excel会计记账软件 编辑:程序博客网 时间:2024/06/06 19:34

首先除法的要注意corner case 0,1之类的

然后就是另一个方法是数bit 1 的个数 2的power只能有一个1 

public class Solution {    public boolean isPowerOfTwo(int n) {        return n>0 && Integer.bitCount(n) == 1;    }}
return ((n & (n-1))==0 && n>0);



0 0
原创粉丝点击