Apply for 5 12M memory.
One bit represents an unsigned int value.
Read in 4 billion numbers and set the corresponding bits.
Read in the number to be queried, and check whether the corresponding bit is 1, 1 means it exists, and 0 means it does not exist.
Secondly, the bitmap method is used to judge whether there are duplicates in the plastic array.
Judging whether there is duplication in a collection is one of the common programming tasks. When the amount of data in the collection is large, we usually want to scan less times, so the double loop method is not desirable.
Bitmap method is more suitable for this situation. The method is to create a new array with the length of max+ 1 according to the largest element in the set, and then scan the original array again, and give the location of the new array 1 after several encounters. If 5 is satisfied, give the sixth element of the new array 1, so that the next time you want to set it, you will find that the sixth element of the new array is 65438. This method of initializing a new array after zero is similar to bitmap method, so it is called bitmap method. Its worst number of operations is 2N. If the maximum value of the array is known and the length of the new array can be fixed in advance, the efficiency can be doubled.
The sample code is as follows:
Package com.sitinspring
Public class DuplicatedArrayTest {
Public static void main(String[] args) {
int [][] arr = {
{ 1 , 2 , 3 , 5 , 3 , 5 , 56, 534 , 3 , 32 } ,
{ 1 , 2 , 3 , 5 } ,
{ 1 , 2 , 3 , 5 , 3 , 5 } ,
{ 0 , 0 , 1 , 2 , 3 , 5 , 56, 534 , 78 , 32 } ,
} ;
for(int I = 0; I < array length; i ++ ) {
System.out.print (array:);
for ( int temp:arr[i]) {
System.out.print(temp +,);
}
System.out.print (middle);
system . out . print(hasDuplicatedItem(arr[I])? Existence: does not exist);
System.out.print (repeating element. \ n);
}
}
/**
* judging whether there is duplicate data in the shaping array, and the time complexity is O(n)
* @param array
* @ Return
*/
Public static Boolean hasduplicated Editem (int [] arr) {
//Scan the array to find the maximum value.
int max = arr[0];
for(int I = 1; I < array length; i ++ ) {
if(arr[I]& gt; Max) {
max = arr[I];
}
}
//Create a new array according to the maximum value.
int[]bitArray = new int[max+ 1];
//Add a value to the new array by value. For example, if the value is 3, bitArray[3]= 1.
for ( int value:arr) {
if (bitArray[value]! = 0 ) {
//If the position pointed by value is not zero, it means that this block has been set to 1 before, and returning true immediately means that the array is duplicate.
Return true
}
Otherwise {
//If the position pointed by value is zero, it is set to 1, indicating that the bit already exists.
bitArray[value]= 1;
}
}
Returns false
}
}
Output:
There are duplicate elements in the array: 1, 2, 3, 5, 3, 5, 56, 534, 3, 32.
There are no duplicate elements in the array: 1, 2, 3, 5.
There are duplicate elements in the array: 1, 2, 3, 5, 3, 5.
There are duplicate elements in the array: 0,0, 1, 2,3,5,56,534,78,32.
Thirdly, the plastic arrays are sorted by bitmap method.
Bao com.heyang;
Public class BitmapSorter {
Public static void main(String[] args) {
int [] arr = { 1,7,- 3,0,0,6,6,9,- 1 1 };
bitmapSort(arr);
for ( int i:arr) {
System.out.print(i +,);
}
}
/**
* use bitmap method to sort.
* @param array
*/
public static void bitmapSort(int[]arr){
//Find the largest value in the array.
int max = arr[0];
int min = max
for ( int i:arr) {
if(max & lt; i) {
max = I;
}
if(min & gt; i) {
min = I;
}
}
//Get bitmap array
int[]new arr = new int[max-min+ 1];
for ( int i:arr) {
int index = I-min;
new arr[index]++;
}
//Reorganize the elements in the arr
int index = 0;
for(int I = 0; I & ltnewArr.lengthi ++) {
while(new arr[I]& gt; 0 ) {
arr[index]= I+min;
index++;
new arr[I]-;
}
}
}
}
Fourthly, the bitmap method stores data.
How to store unsigned short data in 8k bytes of memory space?
General practice:
Define arrays: unsigned shortcuts [4096];
In this way, only 4K unsigned short data can be stored at most.
Use bitmap method:
Define an array: unsigned chararrbit [8192];
This can store 8K*8=64K unsigned short data.
Byte position and bit position of rrBit (bytes 0~8 19 1, bits 0~7)
For example, write 1234, byte order:1234/8 =154; Sequence:1234&; 0b 11= 2, then 1234 is placed in the subscript 154 byte of arrBit, and the second bit (0~7) of this byte is set to1.
Byte position: int nbyte pos =1234/8 =154;
Bit position: int nbit pos =1234&7 = 2;
//Set the 2 position of 154 bytes of the array to 1.
Unsigned short value =1< & ltnBitPos
arr bit[nBytePos]= arr bit[nBytePos]| val; //Write 1234 to get arrbit [154] = 0b00000100.
Write 1236 at this time.
Byte position: int nbyte pos =1236/8 =154;
Bit position: int nBitPos = 1236 & 7 = 4.
. ///Set 4 bits of 154 bytes of the array to 1.
val = 1 & lt; & ltnBitPos
arr bit[nBytePos]= arr bit[nBytePos]| val; //Write 1236 again to get arrbit [154] = 0b0010100.
Reading data elements: read the arrBit bit by bit to get the byte position and bit position of 1 bit. The element value is 8*nBytePos+nBitPos.
for(I = 0; I<8 192; i++)
{
for(j = 0; j & lt8; j++)
{
if(arr bit[I]& amp; ( 1 & lt; & ltj))
{
cout & lt& ltarr bit:& lt; & lt I<<<j<<<8 * I+j < & ltendl
}
}
}
Will output:
arrBit: 154 2 1234
arrBit: 154 4 1236
Delete element: calculate the byte position and bit position of the element to be deleted: arrbit [nbyte pos]&; = ~( 1 & lt; & ltnbit pos);
For example, delete1234: arrbit [154]&; = ~( 1 & lt; & lt2);