Cracking the coding interview--Q12.4

2014-11-24 10:47:54 · 作者: · 浏览: 1

题目

原文:

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 4KB of memory available, how would you print all duplicate elements in the array

译文:

你有一个存有1到N所有数据的数组,N最大为32,000。这个数组可能有重复的元素,你并不知道N是什么。仅仅4kb的内存可用,怎样打印出这个数组中所有重复的元素。

解答

4kb的内存有4*1024*8=32768>32000,所以可以用Bit Map来处理,代码如下:

class Q12_4{

	public static void print_duplicates(int[] a,int n,int bitsize){
		BitMap bm=new BitMap(bitsize);
		for(int i=0;i
  
   
---EOF---