位图
详解过程参考https://mp.weixin.qq.com/s/xxauNrJY9HlVNvLrL5j2hg
实现代码:
public class BitMap { // Java中char类型占16bit,也即是2个字节 private char[] bytes; private int nbits; public BitMap(int nbits) { this.nbits = nbits; this.bytes = new char[nbits/16+1]; } public void set(int k) { if (k > nbits) return; int byteIndex = k / 16; //这就是图解算法里说的word int bitIndex = k % 16; //这里是偏移量 bytes[byteIndex] |= (1 << bitIndex); } public boolean get(int k) { if (k > nbits) return false; int byteIndex = k / 16; int bitIndex = k % 16; return (bytes[byteIndex] & (1 << bitIndex)) != 0; } }