您的位置:首页 > 其它

Leetcode231 Power of Two

2015-09-01 23:08 375 查看
Given an integer, write a function to determine if it is a power of two.

public class Solution231 {
    public boolean isPowerOfTwo(int n) {

        if(n==1) return true;
        if(n%2 == 1) return false;

        int cnt = 2;

        while (cnt <= Integer.MAX_VALUE) {

            if((cnt ^ n) == 0) {
                return true;
            }
            if(cnt > n || cnt <= 0) return false;
            cnt *= 2;
        }

        return false;
    }
    public static void main(String[] args) {
        Solution231 ans = new Solution231();
        System.out.println(ans.isPowerOfTwo(6));

    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: