【本文链接】

http://www.cnblogs.com/hellogiser/p/using-bitset-to-print-duplicate-elements-of-array.html

【题目】

  一个数组有L个元素,取值范围为0到N,其中N<32000,但是不知道N的确切大小。L个元素中有若干个重复元素,只有4KB的内存,如何输出重复元素?

【分析】

  由于数组的取值在一个范围range[1,32000)之间,我们很自然想到用Bitset来处理。使用Bitset,那么1个整数可以使用1个Bit来表示。1byte能够表示8个不同的整数,4kb能够表示32kb个不同整数。因为32kb=32*1024>32000,那么4kb的内存足够表示32000个不同数字。核心在于Bitset中1个整数的存取。开辟一个能够存储N/32+1个int数字的数组,那么对于数字x存储在array[x/32]这个整数的第x%32个bit位。

  即令i=x/32,j=x%32, array[i] |= (1<<j)

  等价于i=x>>5,j=x&(0x1F)

【代码】

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
 
// 74_Bitset.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "iostream"
#include <ctime>
#include <algorithm>
using namespace std;

class BitSet
{
public:
    BitSet (int range)
    {
        // [0,range)
        m_nRange = range;
        m_nLength = m_nRange / ;

bitset = new int[m_nLength];
        // init all with 0
; i < m_nLength; i++)
        {
            bitset[i] = ;
        }
    }

~BitSet()
    {
        delete []bitset;
    }

void Set(int number)
    {
        ;
        ;
        bitset[i] |= ( << j);
    }
    bool Get(int number)
    {
        ;
        ;
        ;
    }

void Output()
    {
        ; i < m_nRange; i++)
        {
            if (Get(i))
            {
                cout << i << " ";
            }
        }
        cout << endl;
    }
private:
    int *bitset;
    int m_nRange; // range of numbers
    int m_nLength; // len of array
};

void print(int *a, int n)
{
    ; i < n; i++)
    {
        cout << a[i] << " ";
    }
    cout << endl;
}

void PrintDuplicateNumbers(int *a, int n, int count)
{
    cout << "Array numbers======================\n";
    print(a, n);
    cout << "Duplicate numbers======================\n";
    BitSet bs(count);
    ; i < n; i++)
    {
        if (bs.Get(a[i]))
            cout << a[i] << " ";
        else
            bs.Set(a[i]);
    }
    cout << endl;
    cout << "Existing numbers======================\n";
    bs.Output();
}

void test_defualt()
{
    ;
    ;

srand((unsigned int)time(NULL));
    int a[n];
    ; i < n; i++)
    {
        a[i] = rand() % range;
    }
    PrintDuplicateNumbers(a, n, range);
}

int _tmain(int argc, _TCHAR *argv[])
{
    test_defualt();
    ;
}
/*
Array numbers======================
7 0 2 8 0 3 0 3 2 1 7 5 11 5 4 11 1 0 2 4
Duplicate numbers======================
0 0 3 2 7 5 11 1 0 2 4
Existing numbers======================
0 1 2 3 4 5 7 8 11
*/

【本文链接】

http://www.cnblogs.com/hellogiser/p/using-bitset-to-print-duplicate-elements-of-array.html

74 使用BitSet输出数组中的重复元素的更多相关文章

  1. Java-Runoob-高级教程-实例-数组:10. Java 实例 – 查找数组中的重复元素-un

    ylbtech-Java-Runoob-高级教程-实例-数组:10. Java 实例 – 查找数组中的重复元素 1.返回顶部 1. Java 实例 - 查找数组中的重复元素  Java 实例 以下实例 ...

  2. 去掉有序数组中的重复元素 c/c++

    去掉有序数组中的重复元素: int RemoveDuplates(int A[], int nCnt) { ; ; , j = ; i < nCnt && j < nCnt ...

  3. 【leetcode-82,83,26,80】 删除排序链表/数组中的重复元素

    83. 删除排序链表中的重复元素 (1 pass) 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 示例 1: 输入: 1->1->2 输出: 1->2 示例 2: ...

  4. LeetCode#26 | Remove Duplicates from Sorted Array 删除有序数组中的重复元素

    一.题目 Description Given a sorted array, remove the duplicates in-place such that each element appear ...

  5. 获取JS数组中所有重复元素

    //获取数组内所有重复元素,并以数组返回 //例:入参数组['1','2','4','7','1','2','2'] 返回数组:['1','2'] function GetRepeatFwxmmc(a ...

  6. 26. Remove Duplicates from Sorted Array(删除排序数组中的重复元素,利用排序的特性,比较大小)

      Given a sorted array, remove the duplicates in-place such that each element appear only once and r ...

  7. 计蒜客--移除数组中的重复元素 (set)

    给定一个升序排列的数组,去掉重复的数,并输出新的数组的长度. 例如:数组 A = \{1, 1, 2\}A={1,1,2},你的程序应该输出 22 即新数组的长度,新数组为 \{1, 2\}{1,2} ...

  8. JSK 11: 移除数组中的重复元素

    题目描述 给定一个升序排列的数组,去掉重复的数,并输出新的数组的长度. 例如:数组 $A = \{1, 1, 2\}$,你的程序应该输出 $2$ 即新数组的长度,新数组为 $\{1, 2\}$. 要求 ...

  9. 010-PHP输出数组中第某个元素

    <?php $monthName = array(1 => "January", "February", "March",//初 ...

随机推荐

  1. 【HDU 2222】Keywords Search AC自动机模板题

    参考iwtwiioi的模板写出来的.上午gty讲的并没有听懂,只好自己慢慢对着模板理解. 在HDU上为什么相同的程序提交有时T有时A!!! 奉上sth神犇的模板(不是这道题): var ch:char ...

  2. formData_html5_map标签

    1 : //更省事 var files = fileInput.files; var formData = new FormData(); //将所有文件插入formData formData .ap ...

  3. hdu1247 字典树

    开始以为枚举会超时,因为有50000的词.后来试了一发就过了.哈哈.枚举没一个单词,将单词拆为2半,如果2半都出现过,那就是要求的. #include<stdio.h> #include& ...

  4. 41.Android之图片放大缩小学习

    生活中经常会用到图片放大和缩小,今天简单学习下. 思路:1.添加一个操作图片放大和缩小类;  2. 布局文件中引用这个自定义控件;  3. 主Activity一些修改. 代码如下: 增加图片操作类: ...

  5. 似然估计中为什么要取对数以GMM为例

    1.往往假设特征之间独立同分布,那么似然函数往往是连城形式,直接求骗到不好搞,根据log可以把连乘变为连加. 2.另外概率值是小数,多个小数相乘容易赵成浮点数下溢,去log变为连加可以避免这个问题. ...

  6. Spring学习8-Spring事务管理(注解式声明事务管理)

    步骤一.在spring配置文件中引入<tx:>命名空间 <beans xmlns="http://www.springframework.org/schema/beans& ...

  7. 初学JDBC,防SQL注入简单示例

    在JDBC简单封装的基础上实现 public class UserDao{ public static void testGetUser(String userName) throws Excepti ...

  8. hdu 2050 折线分割平面

    训练递推用题,第一次做这个题,蒙的,而且对了. #include <stdio.h> int main(void) { int c,a; scanf("%d",& ...

  9. dedecms首页调用栏目内容和单页内容的方法

    常用的需要调到首页来的单页内容,比如企业简介.联系我们等等内容,我们在首页可能都要进行体现.通过常规的方式,包括查阅dede官方论坛资料,都找不到比较合适的答案.今天我们就提供两种方式进行调用. 我们 ...

  10. 在xib里,拖一个UIView到UITableView中作为tableHeaderView

    原贴地址:http://blog.csdn.net/haoxinqingb/article/details/41683881 内容 在xib里,拖一个UIView到UITableView中作为tabl ...