Skip to content

Commit d533dc6

Browse files
author
chenweijie
committed
bitmap
1 parent f2431df commit d533dc6

6 files changed

Lines changed: 269 additions & 0 deletions

File tree

‎src/main/java/com/chen/algorithm/znn/bitmap/Test.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,13 @@
66
* @Description: 位运算相关
77
*/
88
public class Test {
9+
/*
10+
位运算操作:
11+
1、 x & 1 == 1 or x & 1 == 0 判断奇偶(x % 2 == 1)
12+
2、 x = x & (x-1) =>清零最低位的 1,即将最低位的1设置为0
13+
3、 x & -x => 得到最低位的 1
14+
4、任何数和 0 做异或运算,结果仍然是原来的数,即 a⊕0=a。
15+
5、任何数和其自身做异或运算,结果是 0,a⊕a=0。
16+
6、异或运算满足交换律和结合律,即 a⊕b⊕a=b⊕a⊕a=b⊕(a⊕a)=b⊕0=b。
17+
*/
918
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.chen.algorithm.znn.bitmap.test136;
2+
3+
import org.junit.Test;
4+
5+
/**
6+
* https://leetcode-cn.com/problems/single-number/solution/zhi-chu-xian-yi-ci-de-shu-zi-by-leetcode-solution/
7+
*
8+
* @Auther: zhunn
9+
* @Date: 2020/11/07 17:23
10+
* @Description: 只出现一次的数字:1-hash表存储;2-位运算
11+
* 1、任何数和 0 做异或运算,结果仍然是原来的数,即 a⊕0=a。
12+
* 2、任何数和其自身做异或运算,结果是 0,a⊕a=0。
13+
* 3、异或运算满足交换律和结合律,即 a⊕b⊕a=b⊕a⊕a=b⊕(a⊕a)=b⊕0=b。
14+
*/
15+
public class Solution {
16+
17+
/**
18+
* 2-位运算
19+
* @param nums
20+
* @return
21+
*/
22+
public int singleNumber(int[] nums) {
23+
if (nums == null || nums.length == 0) {
24+
return -1;
25+
}
26+
27+
int single = 0;
28+
for (int num : nums) {
29+
single = single ^ num;
30+
}
31+
return single;
32+
}
33+
34+
@Test
35+
public void test() {
36+
int[] nums = {7, 1, 2, 1, 2};
37+
System.out.println(singleNumber(nums));
38+
}
39+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.chen.algorithm.znn.bitmap.test191;
2+
3+
import org.junit.Test;
4+
5+
/**
6+
* https://leetcode-cn.com/problems/number-of-1-bits/solution/wei-1de-ge-shu-by-leetcode/
7+
*
8+
* @Auther: zhunn
9+
* @Date: 2020/11/07 21:23
10+
* @Description: 位1的个数:1-位运算;2-循环和位移动
11+
* 对于任意数字 n ,将 n 和 n−1 做与运算,会把最后一个 1 的位变成 0
12+
*/
13+
public class Solution {
14+
15+
/**
16+
* 1-位运算
17+
*
18+
* @param n
19+
* @return
20+
*/
21+
public int hammingWeight(int n) {
22+
int sum = 0;
23+
while (n != 0) {
24+
sum++;
25+
n = n & (n - 1); // 将数字n最后一位1变为0
26+
}
27+
return sum;
28+
}
29+
30+
public int hammingWeight2(int n) {
31+
32+
int sum = 0;
33+
int mask = 1;
34+
for (int i = 0; i < 32; i++) {
35+
if ((n & mask) != 0) {
36+
sum++;
37+
}
38+
mask = mask << 1;
39+
}
40+
return sum;
41+
}
42+
43+
@Test
44+
public void test() {
45+
System.out.println(hammingWeight(-3));
46+
}
47+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.chen.algorithm.znn.bitmap.test231;
2+
3+
import org.junit.Test;
4+
5+
/**
6+
* https://leetcode-cn.com/problems/power-of-two/solution/2de-mi-by-leetcode/
7+
*
8+
* @Auther: zhunn
9+
* @Date: 2020/11/07 22:23
10+
* @Description: 2的幂:1-位运算-获取二进制中最右边的 1(x&(-x));2-位运算-去除二进制中最右边的 1 (x&(x-1))
11+
* 2 的幂二进制表示只含有一个 1。
12+
*/
13+
public class Solution {
14+
15+
/**
16+
* 1-位运算-获取二进制中最右边的 1(x&(-x))
17+
*
18+
* @param n
19+
* @return
20+
*/
21+
public boolean isPowerOfTwo(int n) {
22+
if (n == 0) {
23+
return false;
24+
}
25+
26+
long x = (long) n;
27+
return (x & (-x)) == x;
28+
}
29+
30+
/**
31+
* 2-位运算-去除二进制中最右边的 1 (x&(x-1))
32+
*
33+
* @param n
34+
* @return
35+
*/
36+
public boolean isPowerOfTwo2(int n) {
37+
if (n == 0) {
38+
return false;
39+
}
40+
long x = (long) n;
41+
return (x & (x - 1)) == 0;
42+
}
43+
44+
@Test
45+
public void test() {
46+
System.out.println(isPowerOfTwo(16));
47+
}
48+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.chen.algorithm.znn.bitmap.test338;
2+
3+
import com.alibaba.fastjson.JSON;
4+
import org.junit.Test;
5+
6+
/**
7+
* https://leetcode-cn.com/problems/counting-bits/solution/bi-te-wei-ji-shu-by-leetcode/
8+
*
9+
* @Auther: zhunn
10+
* @Date: 2020/11/07 22:23
11+
* @Description: 比特位计数:1-位运算-Pop count;2-动态规划+最后设置位;3-动态规划+最高有效位;4-动态规划+最低有效位;
12+
*/
13+
public class Solution {
14+
15+
/**
16+
* 1-位运算-Pop count
17+
*
18+
* @param num
19+
* @return
20+
*/
21+
public int[] countBits(int num) {
22+
int[] res = new int[num + 1];
23+
24+
for (int i = 1; i <= num; i++) {
25+
res[i] = popCount(i);
26+
}
27+
return res;
28+
}
29+
30+
private int popCount(int n) {
31+
int count = 0;
32+
while (n != 0) {
33+
count++;
34+
n = n & (n - 1);
35+
}
36+
return count;
37+
}
38+
39+
/**
40+
* 2-动态规划+最后设置位
41+
*
42+
* @param num
43+
* @return
44+
*/
45+
public int[] countBits2(int num) {
46+
int[] res = new int[num + 1];
47+
for (int i = 1; i <= num; i++) {
48+
res[i] = res[i & (i - 1)] + 1;
49+
}
50+
return res;
51+
}
52+
53+
@Test
54+
public void test() {
55+
System.out.println(JSON.toJSONString(countBits(2)));
56+
System.out.println(JSON.toJSONString(countBits2(5)));
57+
}
58+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.chen.algorithm.znn.bitmap.test461;
2+
3+
import org.junit.Test;
4+
5+
/**
6+
* https://leetcode-cn.com/problems/hamming-distance/solution/yi-ming-ju-chi-by-leetcode/
7+
*
8+
* @Auther: zhunn
9+
* @Date: 2020/11/07 22:23
10+
* @Description: 汉明距离:1-内置位计数功能;2-移位;3-布赖恩·克尼根算法(x&(x-1))
11+
*/
12+
public class Solution {
13+
14+
/**
15+
* 1-内置位计数功能
16+
*
17+
* @param x
18+
* @param y
19+
* @return
20+
*/
21+
public int hammingDistance(int x, int y) {
22+
return Integer.bitCount(x ^ y);
23+
}
24+
25+
/**
26+
* 2-移位
27+
*
28+
* @param x
29+
* @param y
30+
* @return
31+
*/
32+
public int hammingDistance2(int x, int y) {
33+
int dis = x ^ y;
34+
int res = 0;
35+
36+
while (dis != 0) {
37+
if ((dis & 1) == 1) {
38+
res++;
39+
}
40+
dis = dis >> 1;
41+
}
42+
return res;
43+
}
44+
45+
/**
46+
* 3-布赖恩·克尼根算法(x&(x-1))
47+
*
48+
* @param x
49+
* @param y
50+
* @return
51+
*/
52+
public int hammingDistance3(int x, int y) {
53+
int dis = x ^ y;
54+
int res = 0;
55+
while (dis != 0) {
56+
res++;
57+
dis = dis & (dis - 1);
58+
}
59+
return res;
60+
}
61+
62+
@Test
63+
public void test() {
64+
System.out.println(hammingDistance(1, 4));
65+
System.out.println(hammingDistance2(1, 4));
66+
System.out.println(hammingDistance3(1, 4));
67+
}
68+
}

0 commit comments

Comments
 (0)