4.30LeetCode

五一快乐!!!

今天只做了三道ez,给自己放个假

461. Hamming Distance

  1. 汉明距离
1
2
3
4
5
6
7
8
9
10
11
class Solution {
public int hammingDistance(int x, int y) {
int z=x^y;
int res=0;
while(z!=0){
res++;
z=z&(z-1);
}
return res;
}
}

190. Reverse Bits

  1. 颠倒二进制位
1
2
3
4
5
6
7
8
9
10
public class Solution {
public int reverseBits(int n) {
int rs = 0;
for (int i = 0; i < 32; i++) {
rs <<= 1;
rs -= n << 31 - i >> 31;
}
return rs;
}
}

268. Missing Number

  1. 缺失数字
1
2
3
4
5
6
7
8
9
10
11
class Solution {
public int missingNumber(int[] nums) {
int res=0;
for(int i=0;i<nums.length;i++){
res^=nums[i];
res^=i;
}
res^=nums.length;
return res;
}
}
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×