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