提交 ab6bd06d 编写于 作者: GreyZeng's avatar GreyZeng

update code

上级 800f44ff
......@@ -22,6 +22,8 @@
[链表K个节点的组内逆序调整问题](https://www.cnblogs.com/greyzeng/p/17859529.html)
[位图的使用与实现](https://www.cnblogs.com/greyzeng/p/16634282.html)
## 更多
[算法和数据结构学习笔记:CSDN](https://blog.csdn.net/hotonyhui/category_1250716.html)
......@@ -30,8 +32,6 @@
[算法和数据结构学习代码: Github](https://github.com/GreyZeng/algorithm)
[算法和数据结构学习代码: GitCode](https://gitcode.net/hotonyhui/algorithm)
## 参考资料
[算法和数据结构基础班-左程云](https://ke.qq.com/course/2145184)
\ No newline at end of file
package git.snippet.bit;
import java.util.HashSet;
import java.util.Set;
// 位图
// 笔记:https://www.cnblogs.com/greyzeng/p/16634282.html
// 位图
public class BitMap {
private final long[] bits;
// 初始化
public BitMap(int max) {
// 准备多少个整数? 0 ~ 63 需要1个整数
// >> 6 就是 除以 64 >> 效率比除法高
bits = new long[(max + 64) >> 6];
}
public void add(int num) {
// bits[num / 64] |= (1L << (num % 64));
// num % 64 ---> num & 63 即:0b111111
// 只适用于 2 的 n 次方
// 注意:这里是1L非1,如果是1,因为要管64位
bits[num >> 6] |= (1L << (num & 0b111111));
}
public void remove(int num) {
bits[num >> 6] &= ~(1L << (num & 0b111111));
}
public boolean contains(int num) {
return (bits[num >> 6] & (1L << (num & 0b111111))) != 0;
}
}
\ No newline at end of file
package git.snippet.bit;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.util.HashSet;
import java.util.Set;
// 位图
// 笔记:https://www.cnblogs.com/greyzeng/p/16634282.html
public class Code_BitMap {
import static org.junit.jupiter.api.Assertions.*;
public static void main(String[] args) {
System.out.println("test begin");
@DisplayName("位图测试")
public class BitMapTest {
@Test
void testBitMap() {
// System.out.println("test begin");
int max = 70000;
BitMap bitMap = new BitMap(max);
Set<Integer> set = new HashSet<>();
......@@ -23,46 +27,11 @@ public class Code_BitMap {
bitMap.remove(num);
set.remove(num);
} else {
if (bitMap.contains(num) != set.contains(num)) {
System.out.println("Oops!");
break;
}
assertEquals(bitMap.contains(num), set.contains(num));
}
}
for (int num = 0; num <= max; num++) {
if (bitMap.contains(num) != set.contains(num)) {
System.out.println("Oops!");
}
}
System.out.println("test end");
}
// 位图
public static class BitMap {
private final long[] bits;
// 初始化
public BitMap(int max) {
// 准备多少个整数? 0 ~ 63 需要1个整数
// >> 6 就是 除以 64 >> 效率比除法高
bits = new long[(max + 64) >> 6];
}
public void add(int num) {
// bits[num / 64] |= (1L << (num % 64));
// num % 64 ---> num & 63 即:0b111111
// 只适用于 2 的 n 次方
// 注意:这里是1L非1,如果是1,因为要管64位
bits[num >> 6] |= (1L << (num & 0b111111));
}
public void remove(int num) {
bits[num >> 6] &= ~(1L << (num & 0b111111));
}
public boolean contains(int num) {
return (bits[num >> 6] & (1L << (num & 0b111111))) != 0;
assertEquals(bitMap.contains(num), set.contains(num));
}
}
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册