10.4 You have an array with all the numbers from 1 to N, where N is at most 32,000. The array may have duplicate entries and you do not know what N is. With only 4 kilobytes of memory available, how would you print all duplicate elements in the array?

这道题给了我们很多在区间[1, 32000]中的数,让我们只用4KB的内存大小来找出所有的重复项。跟之前那道题很类似10.3 Integer not Contain in the File 文件中不包含的数,还是需要用位向量Bit Vector来解,4KB内存共有215个位Bit,大于32000个数,所以我们可以用每个bit来表示一个数,我们需要一个位向量类BitSet,来建立和数字之间的映射,有点像哈希表的功能,是一个整型数组,由于int型最大可以表示232,所以一个位置就可以映射232个数字,参见代码如下:

class BitSet {
public:
vector<int> _bitset;
BitSet(int size) {
_bitset.resize((size >> ) + );
}
bool get(int pos) {
int wordNum = (pos >> );
int bitNum = (pos % );
return (_bitset[wordNum] & ( << bitNum)) != ;
}
void set(int pos) {
int wordNum = (pos >> );
int bitNum = (pos % );
_bitset[wordNum] |= << bitNum;
}
}; class Solution {
public:
void checkDuplicates(vector<int> array) {
BitSet *bs = new BitSet();
for (int i = ; i < array.size(); ++i) {
int num = array[i];
int num0 = num - ;
if (bs->get(num0)) {
cout << num << endl;
} else {
bs->set(num0);
}
}
}
};

[CareerCup] 10.4 Find All Duplicates Elements 寻找所有的重复项的更多相关文章

  1. [CareerCup] 10.6 Find Duplicate URLs 找重复的URL链接

    10.6 You have 10 billion URLs. How do you detect the duplicate documents? In this case, assume that ...

  2. [CareerCup] 2.1 Remove Duplicates from Unsorted List 移除无序链表中的重复项

    2.1 Write code to remove duplicates from an unsorted linked list.FOLLOW UPHow would you solve this p ...

  3. [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  4. [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项

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

  5. LeetCode 442. Find All Duplicates in an Array (在数组中找到所有的重复项)

    Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others ...

  6. [LeetCode] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)

    [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, remove the d ...

  7. [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

    Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...

  8. [LeetCode] 26. Remove Duplicates from Sorted Array 有序数组中去除重复项

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

  9. [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项 II

    Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...

随机推荐

  1. 那些Xcode不能错过的插件

    转载来自网络   古人云“工欲善其事必先利其器”,打造一个强大的开发环境,是立即提升自身战斗力的绝佳途径!以下是搜集的一些有力的XCode插件.   1.全能搜索家CodePilot 2.0 你要找的 ...

  2. TCP面向连接网络编程

    一 TCP&UDP协议 TCP,Tranfer Control Protocol,是一种面向连接的保证可靠传输的协议.通过TCP协议传输,得到的是一个顺序的无差错的数据流.发送方和接收方的成对 ...

  3. Struts2-tomcat报错:There is no Action mapped for namespace / and action

    HTTP Status 404 - There is no Action mapped for namespace / and action name first. type Status repor ...

  4. 一个完整的WSDL文档及各标签详解

    <?xml version="1.0" encoding="UTF8" ?> <wsdl:definitions targetNamespac ...

  5. confluence wiki搭建使用

    1.准备工作 服务器环境:centos6.6x64 IP:172.16.0.203 1)软件包,地址下载 http://pan.baidu.com/s/1ntlBCQP  ,把几个 软件包放在服务器上 ...

  6. python collections,函数等笔记

    笔记 # -*- coding:utf-8 -*- #需求:一个列表里大于66的元素添加字典的第二个key k2里,小于66的添加第一个 key k1里 li = [1,2,3,4,5,6,67,7, ...

  7. linux进程间通信-有名管道(FIFO)

    有名管道(FIFO) 命名管道也被称为FIFO文件,是一种特殊的文件.由于linux所有的事物都可以被视为文件,所以对命名管道的使用也就变得与文件操作非常统一. (1)创建命名管道 用如下两个函数中的 ...

  8. 用UNIX消息队列实现IPC(以ATM为例)

    清明假期三天没出寝室的门,先是把独立的博客折腾好了.域名备案还没好.域名是ilovecpp.com,意为“我爱C++”,好羞涩,掩面,逃:).话说cnblogs.com的界面好丑 .其余大部分时间就是 ...

  9. makefile自动生成目标与依赖的关系

    有main.c: #include <stdio.h> #include "command.h" int main(int argc, const char *argv ...

  10. WCF宿主端检验队列

    ServiceHost host = new ServiceHost(typeof(Service1)); if (MessageQueue.Exists(@".\private\MyMes ...