題目
原文:
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---