231.Power of Two

时间:2020-05-12 00:37:45   收藏:0   阅读:49

判断一个数是否是2为底的幂次函数。
Input: 16
Output: true
Explanation: 24 = 16

思路:除余法。
难点:0和负数别忘了。

bool isPowerOfTwo(int n) {
    if (n <= 0)return false;
    while (n > 1) {
        if (n % 2 == 1) return false;
        n = n / 2;
    }
    return true;
}

 

原文:https://www.cnblogs.com/luo-c/p/12873333.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!