【本文链接】

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. 【转】Oracle之物化视图

    原文地址:http://www.cnblogs.com/Ronger/archive/2012/03/28/2420962.html 物化视图是一种特殊的物理表,“物化”(Materialized)视 ...

  2. neutron中的dhcp功能

    1. 分布式dhcp 特点: 1)一个dhcp port对应多个host上的tap设备. 2)基于port event的network与agent的绑定与解绑定,即创建tap设备.namespace. ...

  3. 【HDU 1445】Ride to School

    题 题意 骑自行车,等0时开始最早出发的人,一起出发,然后被别人超过时,就追上去,终点距离是4.5km,速度单位是km/s,求到达的时间(s). 分析 贪心,找0时开始最早到的即可. 代码 #incl ...

  4. POJ2187 Beauty Contest

    Description Bessie, Farmer John's prize cow, has just won first place in a bovine beauty contest, ea ...

  5. Linux System Log Collection、Log Integration、Log Analysis System Building Learning

    目录 . 为什么要构建日志系统 . 通用日志系统的总体架构 . 日志系统的元数据来源:data source . 日志系统的子安全域日志收集系统:client Agent . 日志系统的中心日志整合系 ...

  6. linux c学习笔记----进程创建(fork,wait,waitpid)

    1.pid_t fork(); (1)当一个进程调用了fork 以后,系统会创建一个子进程.这个子进程和父进程不同的地方只有他的进程ID 和父进程ID,其他的都是一样.就象符进程克隆(clone)自己 ...

  7. MyEclipse------黑科技

    自动计算器(+,-,*,/) <form method="post" oninput="o.value = parseInt(a.value) + parseInt ...

  8. Applying Eigenvalues to the Fibonacci Problem

    http://scottsievert.github.io/blog/2015/01/31/the-mysterious-eigenvalue/ The Fibonacci problem is a ...

  9. iptables相关

    ⑴.Iptables规则写法的基本格式是:    Iptables [-ttable] COMMAND chain CRETIRIA -j ACTION   ⑵.Iptables规则相关参数说明:  ...

  10. centos命令

     alt + z 打开终端(自定义命令)  su 切换到root