1486. 数组异或操作
感觉一般也用不到 emmm
灵茶山艾府传送门
推导过程可以结合官网部分观看
重点由两部分的结合
将特定部分转换为常见部分
0到n的异或和表示
2595. 奇偶位数
0x555是十六进制数,转换为二进制为 0101 0101 0101
class Solution {
public int[] evenOddBit(int n) {
int mask = 0x555;
return new int[]{Integer.bitCount(n & mask), Integer.bitCount(n & (mask >> 1))};
}
}
231. 2 的幂
class Solution {
static final int BIG = 1 << 30;
public boolean isPowerOfTwo(int n) {
return n > 0 && (n & (n-1)) == 0;
}
}
342. 4的幂
在2的幂的基础上,加上n%3==1的特性
class Solution {
public boolean isPowerOfFour(int n) {
return n > 0 && (n & (n-1)) == 0 && n % 3 == 1;
}
}
476. 数字的补数
class Solution {
public int findComplement(int num) {
int res = 0;
for(int i = 0 ; num > 0; i++){
int temp = num % 2;
temp = temp == 1? 0:1;
res += temp * Math.pow(2, i);
num >>= 1;
}
return res;
}
}
191. 位1的个数
class Solution {
public int hammingWeight(int n) {
int res = 0;
while(n != 0){
n &= n-1;
res++;
}
return res;
}
}
338. 比特位计数
class Solution {
public int[] countBits(int n) {
int[] res = new int[n + 1];
for(int i = 0 ; i <= n; i++){
res[i] = Integer.bitCount(i);
}
return res;
}
}