-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq10p8.java
More file actions
41 lines (34 loc) · 943 Bytes
/
Copy pathq10p8.java
File metadata and controls
41 lines (34 loc) · 943 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public class q10p8{
public static void main(String[] args){
}
//no need to sort array first.
//using bit voctor can save a lot of space.
//but the restriction is obvious: data range should be clear.
public static void check(int[] arr){
bitSet set = new bitSet(32000);
for(int i = 0; i<arr.length; i++){
int num = arr[i];
int num0 = num - 1; //why?
if(set.get(num0)){
System.out.println("duplication: "+ num);
}else
set.set(num0);
}
}
}
class bitSet{
int[] bs;
public bitSet(int n){
bs = new int[(n>>5)+1]; //why?
}
public void set(int pos){
int page = pos >> 5;
int bit = pos & 0x1f;
bs[page] |= (1<<bit);
}
public boolean get(int pos){
int page = pos >> 5;
int bit = pos & 0x1f;
return (bs[page] & (1<<bit)) == 1;
}
}