昨天看了编程珠玑第一章的内容, 发现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. 学习orm框架及一些看法

    首先说说我对现在主流的ORM框架的一些看法: 优点: 让程序员不再关注数据库细节,专心在业务逻辑上,程序员可以不懂数据库就可以开发系统. 让数据库迁移变的非常方便,如果系统需要更改使用的数据库,直接改 ...

  2. 最短Hamilton路径-状压dp解法

    最短Hamilton路径 时间限制: 2 Sec  内存限制: 128 MB 题目描述 给定一张 n(n≤20) 个点的带权无向图,点从 0~n-1 标号,求起点 0 到终点 n-1 的最短Hamil ...

  3. FZOJ Problem 2103 Bin & Jing in wonderland

                                                                                                        ...

  4. 应用defineProperty简单实现vue的双向数据绑定

    双向数据绑定简易版本如何应用defineProperty的getter setter 方法 有这样HTML片段 <input type="text" id="dem ...

  5. toUpperCase与toLowerCase

    public String toLowerCase()此方法返回的字符串转换为小写. public String toUpperCase()此方法返回的字符串转换为大写. 注:支队英文字符有效,其他字 ...

  6. PAT 甲级 1045 Favorite Color Stripe(DP)

    题目链接 Favorite Color Stripe 题意:给定$A$序列和$B$序列,你需要在$B$序列中找出任意一个最长的子序列,使得这个子序列也是$A$的子序列 (这个子序列的相邻元素可以重复) ...

  7. 专访Nick McKeown:网络领域的游戏颠覆者

    如果要找到一个过去10年在网络领域最热的词汇,那么非SDN(软件定义网络)莫属.在过去的十年间无论是学术机构还是标准组织,无论是电信巨擘还是互联网大厂都成其拥趸. 然而几乎每一件SDN的重大事件都离不 ...

  8. IntelliJ IDEA重构技巧收集

    https://segmentfault.com/a/1190000002488608(重命名文件) http://www.jianshu.com/p/ab298b46bf50(快速生成方法) htt ...

  9. OpenLayers3 动画

    参考文章 openlayers3中三种动画实现

  10. sublime的tab和spaces空格切换的坑

    python是严格要求对齐或者叫缩进的: 使用sublime对python进行编程时,可以使用tab或者空格,但是不能混用.特别是从外面把代码拷贝进sublime的时候,更要注意是否一致. 简单介绍一 ...