不要再出来吓人了,对"北大飞"关于H-1B延期文章的纠错# EB23 - 劳工卡
a*0
1 楼
自己的代码老通不过 不知道为什么
public class LRUCache {
int cap;
//hashmap is for Cache's storage
// arraylist is for item's recency
HashMap myHashMap = new HashMap();
// key-value
ArrayList myArrayList = new ArrayList();// key
public LRUCache(int capacity) {
this.cap = capacity;
}
public int get(int key) {
if(this.myHashMap.get(key) == null)
return -1;
this.myArrayList.remove(new Integer(key));
this.myArrayList.add(key);
return this.myHashMap.get(key).intValue();
}
public void set(int key, int value) {
if(this.myHashMap.size()< this.cap){
if(this.myHashMap.get(key) != null){
this.myHashMap.put(key,value);
this.myArrayList.remove(new Integer(key));
this.myArrayList.add(new Integer(key));
}else{
this.myHashMap.put(key,value);
this.myArrayList.add(new Integer(key));
}
}else{
if(this.myHashMap.get(key) != null){
this.myHashMap.put(key,value);
this.myArrayList.remove(new Integer(key));
this.myArrayList.add(new Integer(key));
}else{
this.myHashMap.remove(myArrayList.get(0));
this.myHashMap.put(key,value);
this.myArrayList.remove(0);
this.myArrayList.add(key);
}
}
}
}
public class LRUCache {
int cap;
//hashmap is for Cache's storage
// arraylist is for item's recency
HashMap
// key-value
ArrayList
public LRUCache(int capacity) {
this.cap = capacity;
}
public int get(int key) {
if(this.myHashMap.get(key) == null)
return -1;
this.myArrayList.remove(new Integer(key));
this.myArrayList.add(key);
return this.myHashMap.get(key).intValue();
}
public void set(int key, int value) {
if(this.myHashMap.size()< this.cap){
if(this.myHashMap.get(key) != null){
this.myHashMap.put(key,value);
this.myArrayList.remove(new Integer(key));
this.myArrayList.add(new Integer(key));
}else{
this.myHashMap.put(key,value);
this.myArrayList.add(new Integer(key));
}
}else{
if(this.myHashMap.get(key) != null){
this.myHashMap.put(key,value);
this.myArrayList.remove(new Integer(key));
this.myArrayList.add(new Integer(key));
}else{
this.myHashMap.remove(myArrayList.get(0));
this.myHashMap.put(key,value);
this.myArrayList.remove(0);
this.myArrayList.add(key);
}
}
}
}