开放寻址法模拟哈希表,M一般开数据范围的2~3倍
import java.io.*;
import java.util.*;
public class Main{
static int M = 200003;
static int[] h = new int[M];
static int INF = 0x3f3f3f3f;
public static void main(String[] args)throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
Arrays.fill(h, -INF);
for(int i = 0; i < n; i ++){
String[] s1 = br.readLine().split(" ");
int x = Integer.parseInt(s1[1]);
if(s1[0].equals("I")){
h[find(x)] = x;
}else{
if(h[find(x)] != x) System.out.println("No");
else System.out.println("Yes");
}
}
}
static int find(int x){
int t = (x % M + M) % M;
while(h[t] != -INF && h[t] != x){
t ++;
if(t == M) t = 0;
}
return t;
}
}
Java自带HashMap
import java.io.*;
import java.util.*;
public class Main{
static Map<Integer,Integer> map = new HashMap<>();
public static void main(String[] args)throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
for(int i = 0; i < n; i ++){
String[] s1 = br.readLine().split(" ");
int x = Integer.parseInt(s1[1]);
if(s1[0].equals("I")){
map.put(x, 1);
}else{
if(map.getOrDefault(x, 0) == 1) System.out.println("Yes");
else System.out.println("No");
}
}
}
}