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

 

posted @ 2020-04-12 16:04  Jinxiaobo0509  阅读(120)  评论(0)    收藏  举报