[CareerCup] 10.4 Find All Duplicates Elements 寻找所有的重复项
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 寻找所有的重复项的更多相关文章
- [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 ...
- [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 ...
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- 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 ...
- [LeetCode] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)
[LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, remove the d ...
- [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 ...
- [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 ...
- [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 ...
随机推荐
- Java眼中的XML--文件读取--2 应用SAX方式解析XML
1.获取一个SAXParserFactory的实例.2.通过factory获取SAXParser实例. 3.新建一个包和继承自DefaultHandler的类. 因为SAX遍历方式,比如便利一个节 ...
- HTTP/HTTPs要点
Http(超文本传输协议)是一个属于应用层的面向对象的协议. HTTP结构 HTTP请求: 请求行:Method-Request URI HTTP-Version(CRLF),eg:GET /form ...
- poj邮局1160
题目是给出V个村庄,取出P个做为邮局,要求每个村庄到邮局的距离和最小. 先考虑只有一个邮局的情况,不管你怎么放邮局和最小的情况总是在中点的位置. 再来考虑P>1的情况: 假设P-1个邮局已经选好 ...
- Effective Java 70 Document thread safety
Principle The presence of the synchronized modifier in a method declaration is an implementation det ...
- Python常见数据结构--列表
列表 Python有6个序列的内置类型,但最常见的是列表和元组. 序列都可以进行的操作包括索引,切片.加.乘.检查成员. 此外,Python已经内置确定序列的长度以及确定最大和最下的元素的方法. ...
- Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境搭建教程
准备篇 一.环境说明: 操作系统:Windows Server 2012 R2 PHP版本:php 5.5.8 MySQL版本:MySQL5.6.15 二.相关软件下载: 1.PHP下载地址: htt ...
- 运行编译后的程序报错 error while loading shared libraries: lib*.so: cannot open shared object file: No such file or directory
运行编译后的程序报错 error while loading shared libraries: lib*.so: cannot open shared object file: No such f ...
- 树形dp--hdu 3534 Tree
Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- mod_rewrite
一.简介 http://www.07fly.net/a/wangluobiancheng/Phpbiancheng/201501/58393.html 二.实现原理: 只有当用户的WE ...
- 广搜+打表 POJ 1426 Find The Multiple
POJ 1426 Find The Multiple Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 25734 Ac ...