-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
PowerOfTwo.cpp
44 lines (38 loc) · 1.16 KB
/
PowerOfTwo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Source : https://leetcode.com/problems/power-of-two/
// Author : Hao Chen
// Date : 2015-07-16
/**********************************************************************************
*
* Given an integer, write a function to determine if it is a power of two.
*
* Credits:Special thanks to @jianchao.li.fighter for adding this problem and creating
* all test cases.
*
*
*
**********************************************************************************/
class Solution {
public:
// count the number fo bits 1, if it only has one, then return true
bool isPowerOfTwo01(int n) {
int cnt = 0; //num of bits 1
for(; n>0; n>>=1){
if ( n & 1 ) {
cnt++;
if (cnt>1) return false;
}
}
return cnt==1;
}
//we notice: 2^n - 1 always be 1111111...
//so, (2^n) & (2^n-1) always be zero
bool isPowerOfTwo02(int n) {
return n<=0 ? false : (n & (n-1)) == 0;
}
bool isPowerOfTwo(int n) {
if (random()%2){
return isPowerOfTwo02(n);
}
return isPowerOfTwo01(n);
}
};