(easy)LeetCode 231.Power of Two

Given an integer, write a function to determine if it is a power of two.

Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

代码如下:

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

  运行结果:

     

原文地址:https://www.cnblogs.com/mlz-2019/p/4692172.html