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. 让eclipse启动时拥有jre

    让eclipse 拥有jre,这样才能启动 eclipse/eclipse.ini 首行加入 -vm /home/liujl/installer/jdk/bin/java

  2. javascripts 实习自动提交表单 onsubmit

    html: <form id="formwb" onsubmit="return setPassword();"> <script> d ...

  3. Effective Java 62 Document all exceptions thrown by each method

    Principle Always declare checked exceptions individually, and document precisely the conditions unde ...

  4. Windows Server 2008 下ASP程序连接ORACLE数据库驱动错误

    今天开发那边升级.改造系统过程中,在测试服务器碰到关于ASP程序连接ORACLE数据库的小问题,虽然是小问题,但是整起来真要命啊,花了不少时间,主要是ASP程序啊,这种上古神器,哥还是当年毕业的时候弄 ...

  5. Jquery Easy UI--datagrid的使用(转)

    第一篇学的是做一个管理的外框,接着就是数据datagrid绑定了,这里我用asp.net mvc3来做的,主要就是熟悉属性.方法. 打开easyui的demo 就可以看到如下一段代码: 和上篇一样cl ...

  6. uboot 2014.04 运行过程记录

    uboot启动流程分析,针对S5PV210 BL1阶段,SPL,u-boot-spl.bin 1.首先运行arch/arm/cpu/armv7/start.S 里面的_start函数,进行异常向量表设 ...

  7. 虚拟机centos6.5 --设置主机名

    vi /etc/sysconfig/network #修改HOSTNAME后面的值,机器名 vi /etc/hosts #设置ip和机器名的对应关系 192.168.12.232 master 192 ...

  8. html初始化

    建站老手都知道,这是为了考虑到浏览器的兼容问题,其实不同浏览器对有些标签的默认值是不同的,如果没对CSS初始化往往会出现浏览器之间的页面差异. 当然,初始化样式会对SEO有一定的影响,但鱼和熊掌不可兼 ...

  9. virtualbox 在window10上的兼容性调整

    更新完windows10后,打开当时的virtualbox 4.3.3已经是最新的啦,打开原来安装的几个虚拟机(hadoop),发现均失败. 打开setting一看,网络一栏有问题,桥接模式的虚拟机都 ...

  10. Hadoop 2.0 中的资源管理框架 - YARN(Yet Another Resource Negotiator)

    1. Hadoop 2.0 中的资源管理 http://dongxicheng.org/mapreduce-nextgen/hadoop-1-and-2-resource-manage/ Hadoop ...