9.4携程笔试
今晚携程第三题 ; 提示我有非法越界 ; 检查不出来 ; 大牛们看一下
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Scanner;
public class Main3{
static Queue<Integer> q=new LinkedList<Integer>();
static Map<Integer,Integer> map=new HashMap<Integer,Integer>();
static int max;
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
max=scan.nextInt();
String str;
int key;
int value;
while(scan.hasNext()){
str=scan.next();
if("p".equals(str)){
key=scan.nextInt();
value=scan.nextInt();
if(map.containsKey(key)){
map.put(key, value);
continue;
}
if(map.size()>=max){
int re=duiLie(key);
map.remove(re);
map.put(key, value);
}else{
q.offer(key);
map.put(key, value);
}
}else if("g".equals(str)){
key=scan.nextInt();
q.remove(key);
q.offer(key);
if(map.get(key)==null)
System.out.println(-1);
else
System.out.println(map.get(key));
}
}
}
public static int duiLie(int x){
int re=-1;
if(q.size()>=max){
re=q.poll();
q.offer(x);
}else{
if(q.contains(x))
q.remove(x);
q.offer(x);
}
return re;
}
}#携程##笔试题目#
查看8道真题和解析