Matriz de mapa de bits

Introducción

¿Cómo saber si hay un cierto número entre diez millones de enteros? La idea simple es crear una matriz con un tamaño de 10 millones y luego recorrer la búsqueda, lo que consume espacio y tiempo. Entonces podemos crear una matriz de mapas de bits.

De hecho, esta matriz byte, int, longpuede ser un tipo de matriz normal , pero cada vez que operamos en uno de los elementos de la matriz. Si desea agregar un número 11, establecemos la matriz de mapa de bits11Porque 1, significa que este número existe.

Por ejemplo, use un bytetipo para implementar una matriz de mapa de bits. Dado que el bytetipo es de ocho bits, podemos tener el tamaño de la matriz 实际需求的容量/8. Si es un inttipo, divida por 32.

y entoncesbytes[0] 就代表了位图的[0, 7]位, byte[1] 就代表了位图的[8, 15]位, 依次...

Sin embargo, dado que comienza desde cero, si desea operar con números negativos, debe agregar uno para offsetque todos los datos no sean negativos, lo cual es conveniente para la operación.

static class MyBitMap {
    
    

    private byte[] bitmap;
    private final int bit = 8;

    public MyBitMap(int size) {
    
    
      if (size <= 0) return;
      int initSize = size / bit + 1;
      bitmap = new byte[initSize];
    }

    public void set(int number) {
    
    
      //除以8,代表这个数字在数组中的下标
      //因为我们要找这个数字在整个位图中位于哪一位,而一个数组元素是8位
      //所以除以8就能找到(它对应的位所在的byte类型数据)在数组中的位置。
      int index = number / bit;
      //number % 8 获取到在这个byte类型中,它处于哪一位
      int position = number % bit;
      //进行或运算,即把number对应的位修改为1。
      bitmap[index] |= 1 << (bit - 1 - position);
    }


    public boolean contains(int number) {
    
    
      //在查找这个数和set方法一样,先获得对应数组中的位置
      int index = number / bit;
      //再获取这个元素中对应位的位置
      int position = number % bit;
      //查看对应位是不是1
      return (bitmap[index] & (1 << (bit - 1 - position))) != 0;
    }
  }

Ejemplo de realización: enlace [LC tiene elementos duplicados]

class Solution {
    
    
  static class MyBitMap {
    
    

    private int[] bitmap;
    private final int bit = 32;

    public MyBitMap(int size) {
    
    
      if (size <= 0) return;
      int initSize = size / bit + 1;
      bitmap = new int[initSize];
    }

    public void set(int number) {
    
    
      int index = number / bit;
      int position = number % bit;
      bitmap[index] |= 1 << (bit - 1 - position);
    }


    public boolean contains(int number) {
    
    
      int index = number / bit;
      int position = number % bit;
      return (bitmap[index] & (1 << (bit - 1 - position))) != 0;
    }
  }

  public boolean containsDuplicate(int[] nums) {
    
    
    MyBitMap map = new MyBitMap((int) 1e7);
    int min = Integer.MAX_VALUE;
    for (int num : nums) {
    
    
      min = Math.min(min, num);
    }
    for (int i = 0; i < nums.length; i++) {
    
    
      if (map.contains(nums[i] - min)) return true;
      map.set(nums[i] - min);
    }
    return false;
  }
}

Supongo que te gusta

Origin blog.csdn.net/qq_42007742/article/details/109310536
Recomendado
Clasificación