Heap:
LRU Cache:
用hashmap和雙向linkedlist結(jié)合做緩存悔据,hashmap使得查詢時(shí)間是O(1),鏈表用來(lái)保存時(shí)間軸
Hash Function 如何實(shí)現(xiàn)
PriorityQueue comparator的實(shí)現(xiàn):
//k 必須要有, 是Integer 不是 int 注意
//這個(gè)是從小到大排序的!
PriorityQueue<Integer> pq = new PriorityQueue<Integer>(k, new Comparator<Integer>() {
public int compare(Integer o1, Integer o2) {
if(o1 > o2) {
return 1;
} else if(o1 < o2) {
return -1;
} else {
return 0;
}
}
});
排序用priorityqueue有奇效,求第k個(gè)大的數(shù)萄窜,前K個(gè)大的數(shù),用一個(gè)minheap
遍歷HashMap:
for(Map.Entry<Integer, Integer> entry : map.entrySet()) {
getKey()
getValue()
Collections.sort(result, Collections.reverseorder())
巧妙 需要學(xué)習(xí)
for (Point p : points) {
pq.add(p);
if (pq.size() > k) {
pq.poll();
}
}
H鼋啊2榭獭!
降序
return o2 - 0o1
int compare(Object o1, Object o2) 返回一個(gè)基本類型的整型
如果要按照升序排序凤类,
則o1 小于o2穗泵,返回-1(負(fù)數(shù)),相等返回0谜疤,01大于02返回1(正數(shù))
如果要按照降序排序
則o1 小于o2佃延,返回1(正數(shù)),相等返回0夷磕,01大于02返回-1(負(fù)數(shù))