231. 2的幂
1 class Solution 2 { 3 public: 4 bool isPowerOfTwo(int n) 5 { 6 return n > 0 && (n & n - 1) == 0; 7 } 8 };
1 class Solution 2 { 3 public: 4 bool isPowerOfTwo(int n) 5 { 6 // n&-n等于n的二进制表示里最右边一个1 7 return n > 0 && (n & -n) == n; 8 } 9 };
Mamba never out

浙公网安备 33010602011771号