昨天看了编程珠玑第一章的内容, 发现bitmap对于统计某一个范围内的整数个数效率很高, 就自己实现了一下:

这是原始的bitmap, 用于统计0~maxSize是否出现, 也可以用于排序

 public class BitMap
{
private int[] arr ;
private final int mask = 0X1F ; //用于表示偏移量
private final int shift = 5 ; //用于表示index处于arr的第几个 public BitMap(int maxSize)
{
arr = new int[(maxSize-1)/32+1] ;
} //将index置1
public void set(int index)
{
//index>>5表示index在int中的第几个
//由于mask为16进制的11111, 所以index&mask的结果就是index在int中的位置
//或运算表示置1 , 与0相与表示置0
arr[index>>5] |= (1<<(index&mask)) ;
} //将index清零
public void clear(int index)
{
arr[index>>5] &= (~(1<<(index&mask))) ;
} //测试index是否为1
public boolean test(int index)
{
return (arr[index>>5] &= (1<<(index&mask))) != 0 ;
}
}

如果每个数字可能出现10次, 那么一个int型就可以表示8个元素, bitmap如下:

 /**
* 每个数字最多出现10次
*/
public class BitMap_FourBit
{
private int[] map ;
private int shift = 3 ;
private int mask = 0X7 ; public BitMap_FourBit(int N)
{
map = new int[(N-1)/4+1] ;
} //加1
public void add(int index)
{
int pos = index>>shift ;//除以8获取他在map中的位置
int offset = offset(index) ;//获取在int中的偏移量 int bit = get(pos , offset) ; clear(pos , offset);
//System.out.println(bit);
bit += (1<<offset) ; //加1 map[pos] |= bit ;
} //清0
public void clear(int index)
{
int pos = pos(index) ;//除以8获取他在map中的位置
int offset = offset(index) ;//获取偏移量 clear(pos , offset);
} //获取
public int get(int index)
{
int pos = pos(index) ;
int offset = offset(index) ;//获取偏移量 return count(get(pos , offset)) ;
} //除以4获取他在map中的位置
private int pos(int index)
{
return index>>shift ;
} //获取在int中位置
private int offset(int index)
{
return (index&mask)<<2 ;
} //统计个数
private int count(int num)
{
int result = 0XF ; if((num & result) != 0)
return result & num ; for(int i=1 ; i<=8 ; i++)
{
result <<= 4 ; if((num & result) != 0)
{
result &= num ;
return result >> (4*i) ;
}
} return 0 ;
} private int get(int pos , int offset)
{
int bit = 0XF<<offset ; return map[pos] & bit ;
} private void clear(int pos , int offset)
{
int bit = 0XF<<offset ;
map[pos] &= (~bit) ; //将原来的位置清0
} public static void main(String[] args) {
BitMap_FourBit bitMap_fourBit = new BitMap_FourBit(16) ; bitMap_fourBit.add(8) ;
bitMap_fourBit.add(8) ;
bitMap_fourBit.add(9) ;
bitMap_fourBit.add(9) ;
bitMap_fourBit.add(9) ;
bitMap_fourBit.add(8) ; bitMap_fourBit.clear(9); System.out.println(bitMap_fourBit.get(8)) ;
System.out.println(bitMap_fourBit.get(9)) ;
}
}

bitmap进行个数统计的更多相关文章

  1. 一步一步写算法(之n!中末尾零的个数统计)

    原文:一步一步写算法(之n!中末尾零的个数统计) [ 声明:版权所有,欢迎转载,请勿用于商业用途.  联系信箱:feixiaoxing @163.com] 在很多面试的题目中,求n!结果中零的个数也是 ...

  2. 华为oj之字符个数统计

    题目:字符个数统计 热度指数:4720 时间限制:1秒 空间限制:32768K 本题知识点: 字符串 题目描述 编写一个函数,计算字符串中含有的不同字符的个数.字符在ACSII码范围内(0~127). ...

  3. php实现 字符个数统计

    php实现 字符个数统计 一.总结 一句话总结: 1.php字符转ascii码函数? ord() 6 if(ord($input[$i]) < 128 and 0 < ord($input ...

  4. Java实现 蓝桥杯VIP 算法提高 不同单词个数统计

    算法提高 不同单词个数统计 时间限制:1.0s 内存限制:512.0MB 问题描述 编写一个程序,输入一个句子,然后统计出这个句子当中不同的单词个数.例如:对于句子"one little t ...

  5. linux makefile字符串操作函数 替换subst、模式替换patsubst、去首尾空格strip、查找字符串findstring、过滤filter、反过滤filter-out、排序函数sort、取单词word、取单词串wordlist、个数统计words

    1.1       字符操作函数使用 在Makefile中可以使用函数来处理变量,从而让我们的命令或是规则更为的灵活和具有智能.make所支持的函数也不算很多,不过已经足够我们的操作了.函数调用后,函 ...

  6. Python2018-字符串中字符个数统计

    1 编写程序,完成以下要求: 统计字符串中,各个字符的个数 比如:"hello world" 字符串统计的结果为: h:1 e:1 l:3 o:2 d:1 r:1 w:1 prin ...

  7. sqlserver 数据库 的数据库个数统计 表个数统计 表的数据量统计(转载)

    http://www.cnblogs.com/qinche/archive/2012/08/09/app.html 由于今天要监控数据,急需统计实例中1有多少库2库里有多少表3每个表有多少数据 --将 ...

  8. OpenJudge计算概论-求字母的个数(统计元音字母个数)

    /*======================================================================= 求字母的个数 总时间限制: 1000ms 内存限制: ...

  9. Java不同单词个数统计

    描述 编写一个程序,输入一个句子,然后统计出这个句子当中不同的单词个数.例如:对于句子“one little two little three little boys”,总共有5个不同的单词:one, ...

随机推荐

  1. BZOJ 2733 [HNOI2012]永无乡 ——线段树 并查集

    用并查集维护联通块. 用线段树的合并来合并联通块. 自己YY了一个写法. #include <map> #include <cmath> #include <queue& ...

  2. [USACO Section 3.2] 01串 Stringsobits (动态规划)

    题目链接 Solution 贼有意思的 DP, 也可以用组合数学做. \(f[i][j]\) 代表前 \(i\) 位,有 \(j\) 个 \(1\) 的方案数. 转移方程很简单 : \(f[i][j] ...

  3. jenkins配置本机JDK和maven环境

    1.jenkins官网下下载jenkins的war包 2.安装jenkins,启动命令:java  -jar jenkins.war 3.打开http://localhost:8080/ 4.点击系统 ...

  4. udp 多播2

    11.3  多播 单播用于两个主机之间的端对端通信,广播用于一个主机对整个局域网上所有主机上的数据通信.单播和广播是两个极端,要么对一个主机进行通信,要么对整个局域网上的主机进行通信.实际情况下,经常 ...

  5. dedecms--二次开发之前后台登录分开

    最近在写dedecms系统下会员功能二次开发,然后发现在本地测试的时候每次登录后台,管理员帐号都会在前台页面也显示登录了,但是如果真的是在前台页面用管理员账号登录的话那是登陆不了的,所以我觉得这样的效 ...

  6. docker部署 mysql redis问题

    问题:(ubuntu不报错,centos报错) ERROR: : starting container process caused "process_linux.go:402: conta ...

  7. hdu 1401(单广各种卡的搜索题||双广秒速)

    Solitaire Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

  8. LeetCode OJ--Permutations *

    https://oj.leetcode.com/problems/permutations/ 写出一列数的全排列 #include <iostream> #include <vect ...

  9. Codeforces Gym101606 C.Cued In (2017 United Kingdom and Ireland Programming Contest (UKIEPC 2017))

    C Cued In 这个题是打球的.都忘了写的什么了... 代码: 1 #include<iostream> 2 #include<cstring> 3 #include< ...

  10. [原创][FPGA]时钟分频之奇分频(5分频)

    0. 简介 有时在基本模块的设计中常常会使用到时钟分频,时钟的偶分频相对与奇分频比较简单,但是奇分频的理念想透彻后也是十分简单的,这里就把奇分频做一个记录. 1. 奇分频 其实现很简单,主要为使用两个 ...