-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
54 lines (47 loc) · 783 Bytes
/
main.cpp
File metadata and controls
54 lines (47 loc) · 783 Bytes
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
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
/***
01交替:
000101010
^ 000001010
= 000100000
***/
class Solution {
public:
bool hasAlternatingBits(int n) {
return !((n ^= n/4) & (n-1));
}
};
/***
01交替:
000101010
^ 000010101
= 000111111
***/
class Solution {
public:
bool hasAlternatingBits(int n) {
return !((n ^= n/2) & n+1);
}
};
// 位运算
/*
class Solution {
public:
bool hasAlternatingBits(int n) {
int d = n & 1; // n的最后一位
while((n&1) == d)
{
d = 1 - d; // 交替,是否为0,1交替
n >>= 1; // 将n右移,判断最后一位
}
return n == 0;
}
};
*/
int main()
{
return 0;
}