[LintCode] O(1)检测2的幂次

 1 class Solution {
 2 public:
 3     /*
 4      * @param n: An integer
 5      * @return: True or false
 6      */
 7     bool checkPowerOf2(int n) {
 8         // write your code here
 9         return n > 0 && ((n & (n - 1)) == 0);
10     }
11 };

 

posted @ 2015-07-01 01:59  jianchao-li  阅读(423)  评论(0编辑  收藏  举报