8.12

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();
            }
        }

!!!
降序
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ù))

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容