231. Power of Two


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

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

本站原创文章皆遵循“署名-非商业性使用-相同方式共享 3.0 (CC BY-NC-SA 3.0)”。转载请保留以下标注:

原文来源:《231. Power of Two》

147
0 0 147

延伸阅读

发表回复

登录后才能评论
分享本页
返回顶部