目录
- XOR - 异或
- 判断奇偶:
- [51. n皇后](https://leetcode-cn.com/problems/n-queens/)
- [52. n皇后II](https://leetcode-cn.com/problems/n-queens-ii/description/)
- [190. 颠倒二进制位](https://leetcode-cn.com/problems/reverse-bits/)
- [191. 位1的个数](https://leetcode-cn.com/problems/number-of-1-bits/)
- [231. 2的幂](https://leetcode-cn.com/problems/power-of-two/)
- [338. 比特位计数](https://leetcode-cn.com/problems/counting-bits/description/)
XOR - 异或
异或:相同我0,不同为1 也可用“不进位加法”来理解。
异或操作的一些特点:
x ^ 0 = x
x ^ 1s = ~x //注意1s = ~ 0
x ^ (~x) = 1s
x ^ x = 0
c = a ^ b => a ^ c = b,b ^ c= a//交换两个数
a ^ b ^ c = a ^ (b ^ c) = (a ^ b) ^ c
判断奇偶:
x % 2 == 1 ——> (x & 1) == 1
x % 2 == 0 ——> (x & 1) == 0
x >> 1 ——> x / 2
即:x = x / 2 ; ——> x = x >> 1;
mid = (left + right)/2;——> mid = (left + right) >> 1;
x = x & (x - 1) 清零最低位的1
x & -x => 得到最低位的1
x &(~x)=> 0
51. n皇后
private int size;
private int count;
private void solve(int row,int ld, int rd){
if (row == size){
count++;
return;
}
int pos = size & (~(row | ld | rd));
while (pos != 0){
int p = pos &(~pos);
pos &= pos - 1;
solve(row