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;
    }
}

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

原文作者:Jake Tao,来源:「231. Power of Two」

147
0 0 147

延伸阅读

发表回复

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