231. Power of Two

来源:互联网 发布:java 合并多个excel 编辑:程序博客网 时间:2024/06/06 09:16

方法1:递归

public class Solution {    public boolean isPowerOfTwo(int n) {        if(n <= 0) return false;        if(n == 1) return true;        if(n>=2 && n%2== 0)            return isPowerOfTwo(n/2);        return false;    }}

方法2:位运算

public class Solution {    public boolean isPowerOfTwo(int n) {        if(n <= 0) return false;        return (n&(n-1)) == 0;    }}



原创粉丝点击