【leetcode】231-power-of-two

孟佳

关注

阅读 62

2022-07-13

problem

​​231-power-of-two​​

 solution1

class Solution {
public:
bool isPowerOfTwo(int n) {
if(n==0) return false;
while(n%2==0)
{
n /= 2;
}
return n==1;
}
};

View Code

solution2

【leetcode】231-power-of-two_其它

【leetcode】231-power-of-two_其它_02

class Solution {
public:
bool isPowerOfTwo(int n) {
if(n<=0) return false;
return !(n&(n-1));
}
};

View Code

 

 

 

参考

1. ​​Leetcode_231-power-of-two​​;

精彩评论(0)

0 0 举报