一般来说int代表一个数字,但是如果利用每一个位 ,则可以表示32个数字 ,在数据量极大的情况下可以显著的减轻内存的负担。我们就以int为例构造一个bitmap,并使用其来解决一个简单的问题:求两个数组的交集

先实现一个bitmap

/**
* @Description:
* @author: zhoum
* @Date: 2020-01-23
* @Time: 10:49
*/
public class BitMap { private int[] sign = {0x00000001,0x00000002,0x00000004,0x00000008,0x00000010,0x00000020,0x00000040,0x00000080,0x00000100,0x00000200,0x00000400,0x00000800,0x00001000,0x00002000,0x00004000,0x00008000,
0x00010000,0x00020000,0x00040000,0x00080000,0x00100000,0x00200000,0x00400000,0x00800000,0x01000000,0x02000000,0x04000000,0x08000000,0x10000000,0x20000000,0x40000000,0x80000000}; private int[] arr ; private int capacity; public BitMap(int capacity) {
validate(capacity);
this.capacity = capacity;
this.arr = new int[(capacity>>5)+1];
} public void put(int k){
if ( k > capacity ){
throw new RuntimeException("k is greater than capacity");
}
validate(k);
int index = k >> 5 ;//当前数字应该存放的bucket索引
arr[index] = arr[index]|sign[k & 31]; } private void validate(int k){
if ( k <= 0 ){
throw new IllegalArgumentException(" capacity must be greater than zero");
}
} public int[] getMixed(BitMap bitMap){
int length = Math.min(bitMap.arr.length,this.arr.length);
int[] other = new int[length],me = new int[length];
System.arraycopy(bitMap.arr,0,other,0,length);
System.arraycopy(this.arr,0,me,0,length);
//借用集合的无固定大小来构建最后数组
List<Integer> result = new ArrayList<>();
for (int i = 0; i < length; i++) {
int k= other[i] & me[i];
for (int j = 1; j <= 32; j++) {
if ( ((k>>j)&1) == 1 ){
result.add((i<<5)+j);
}
}
}
if ( result.size() == 0 ){
return null;
}else {
int[] rs = new int[result.size()];
for (int i = 0; i < result.size(); i++) {
rs[i] = result.get(i);
}
return rs;
} }
}

写一个main方法试验下

    public static void main(String[] args) {
BitMap bitMap = new BitMap(1000){{
put(248);
put(5);
put(9);
put(12);
put(6);
put(13);
put(963); }};
BitMap bitMap1 = new BitMap(1000){{
put(248);
put(15);
put(13);
put(963);
put(5);
put(6);
put(9); }};
int[] mixed = bitMap.getMixed(bitMap1);
System.out.println(Arrays.toString(mixed)); }

得到有序结果

java使用bitmap求两个数组的交集的更多相关文章

  1. java用最少循环求两个数组的交集、差集、并集

    import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.List ...

  2. Java实现 LeetCode 350 两个数组的交集 II(二)

    350. 两个数组的交集 II 给定两个数组,编写一个函数来计算它们的交集. 示例 1: 输入: nums1 = [1,2,2,1], nums2 = [2,2] 输出: [2,2] 示例 2: 输入 ...

  3. Java实现 LeetCode 349 两个数组的交集

    349. 两个数组的交集 给定两个数组,编写一个函数来计算它们的交集. 示例 1: 输入: nums1 = [1,2,2,1], nums2 = [2,2] 输出: [2] 示例 2: 输入: num ...

  4. leetcode-350-Intersection of Two Arrays II(求两个数组的交集)

    题目描述: Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, ...

  5. (C#) 求两个数组的交集

    基本上在面试的时候,会具体到两个int数组,或string数组.具体也就是讨论算法. 首先需要的是和面试的人确认题目的含义,并非直接答题. 然后,可以提出自己的想法,首先最快的是用linq { Lis ...

  6. js求两个数组的交集|并集|差集|去重

    let a = [1,2,3], b= [2, 4, 5]; 1.差集 (a-b 差集:属于a但不属于b的集合)  a-b = [1,3] (b-a 差集:属于b但不属于a的集合)  b-a = [4 ...

  7. 用lua求两个数组的交集、并集和补集。

    -- 克隆 function Clone(object) local lookup_table = { } local function _copy(object) if type(object) ~ ...

  8. js取两个数组的交集|差集|并集|补集|去重示例代码

    http://www.jb51.net/article/40385.htm 代码如下: /** * each是一个集合迭代函数,它接受一个函数作为参数和一组可选的参数 * 这个迭代函数依次将集合的每一 ...

  9. 求两个集合的交集和并集C#

    我是用hashset<T>来实现的 具体如代码所示 using System; using System.Collections.Generic; using System.Linq; u ...

随机推荐

  1. 3 种比较 cmp

    结构体中的比较 struct dian{ int l,r; bool operator <(const dian &t)const { if(r==t.r) return l>t. ...

  2. 随缘记录 LeetCode第168场周赛 2019-12-22

    5292. 划分数组为连续数字的集合 给你一个整数数组 nums 和一个正整数 k,请你判断是否可以把这个数组划分成一些由 k 个连续数字组成的集合. 如果可以,请返回 True:否则,返回 Fals ...

  3. 本地cmd连接远程mysql数据库

    一.登录远程mysql 输入mysql -h要远程的IP地址 -u设置的MySQL用户名 -p登录用户密码 例如:mysql -h 192.168.1.139 -u root -p dorlocald ...

  4. .net c# MVC提交表单的4种方法

    https://blog.csdn.net/qingkaqingka/article/details/85047781

  5. Windows10安装node.js

    1.下去官网下载node.js https://nodejs.org/zh-cn/download/ 2.安装,直接默认即可,安装路径也可以自己选择 3.设置环境变量 1.安装软件,若是-g,则是全局 ...

  6. Bridge(Ad Hoc)

  7. 【转载】Java反射机制详解

    转自:http://baike.xsoftlab.net/view/209.html#3_8 1反射机制是什么 反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对 ...

  8. noobSTL-0-开题报告

    noobSTL-0-开题报告 STL介绍 STL是Standard Template Library的简称,中文名标准模板库. STL是一种泛型编程.面向对象编程关注的是编程的数据方面,而泛型编程关注 ...

  9. VIM学习(转)

    原文:http://www.cnblogs.com/nerxious/archive/2012/12/21/2827303.html 断断续续的使用VIM也一年了,会的始终都是那么几个命令,效率极低 ...

  10. c语言getipaddrtable获得ip地址与sendArp获得mac地址以及一些字节序问题记录

    https://docs.microsoft.com/zh-cn/windows/win32/api/iphlpapi/nf-iphlpapi-getipaddrtable msdn,有很多c的源码还 ...