`

Leetcode - Power of Two

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

public class Solution {
    public boolean isPowerOfTwo1(int n) {
        if (n < 0) return false;
        for (int i = 0; i <32; i++) {
            if (n == (1 << i))
                return true;
        }
        return false;
    }
    public boolean isPowerOfTwo(int n) {
        return n > 0 && (n & (n - 1)) == 0;
    }
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics