码迷,mamicode.com
首页 > 其他好文 > 详细

LintCode 142. O(1)时间检测2的幂次

时间:2018-01-28 21:54:00      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:.com   toggle   返回   als   strong   ogg   hal   public   code   

用 O(1) 时间检测整数 n 是否是 2 的幂次。

 

样例

n=4,返回 true;

n=5,返回 false.

挑战 

O(1) time

 

class Solution {
public:
    /*
     * @param n: An integer
     * @return: True or false
     */
    bool checkPowerOf2(int n) {
        // write your code here
        if(n<1) return false;//1是2的0次幂
         return ((n)&(n-1))==0;
    }
};

 

LintCode 142. O(1)时间检测2的幂次

标签:.com   toggle   返回   als   strong   ogg   hal   public   code   

原文地址:https://www.cnblogs.com/zslhg903/p/8372344.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!