【本文链接】

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. 细菌觅食算法-python实现

    BFOIndividual.py import numpy as np import ObjFunction class BFOIndividual: ''' individual of bateri ...

  2. POJ1745Divisibility(01背包思想)

    Divisibility Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11151   Accepted: 3993 Des ...

  3. ActivityInfo taskAffinity

    通常在Manifest里面使用 <application android:icon="@drawable/icon" android:label="@string/ ...

  4. vs------安装window net.framework 出现严重错误解决方法

    打开system32和SysWOW64修改里面共六个msvcr100_clr0400.dll文件的名字除了带120的文件不用修改 需要修改管理员权限: 步骤:属性->安全->编辑-> ...

  5. 使用migrate.exe执行EF code first 迁移

    Code First 迁移可用于从 Visual Studio 内部更新数据库,但也可通过命令行工具 migrate.exe 来执行.本页简单介绍如何使用 migrate.exe 对数据库执行迁移. ...

  6. DEEP LEARNING WITH STRUCTURE

    DEEP LEARNING WITH STRUCTURE Charlie Tang is a PhD student in the Machine Learning group at the Univ ...

  7. https://github.com/yrs244742688/GeneratePemWithMoAndEx RSA加密

    <RSAKeyValue> <Modulus> xA7SEU+e0yQH5rm9kbCDN9o3aPIo7HbP7tX6WOocLZAtNfyxSZDU16ksL6 Wjuba ...

  8. python之BIF函数在列表中的应用

    1 Python 3.3.4 (v3.3.4:7ff62415e426, Feb 10 2014, 18:13:51) [MSC v.1600 64 bit (AMD64)] on win32 2 T ...

  9. WINDOWS渗透与提权总结(1)

    旁站路径问题: 1.读网站配置. 2.用以下VBS: 01 On Error Resume Next 02   03 If (LCase(Right(WScript.Fullname, 11)) = ...

  10. Java&.Net虚拟机精简(GreenJVM&GreenDotNet发布) .

    精简JRE体积的小工具:http://blog.csdn.net/cping1982/archive/2008/09/02/2865198.aspx 项目地址:http://code.google.c ...