8.12

Heap:

LRU Cache:
用hashmap和双向linkedlist结合做缓存,hashmap使得查询时间是O(1),链表用来保存时间轴

Hash Function 如何实现

PriorityQueue comparator的实现:

        //k 必须要有, 是Integer 不是 int 注意
//这个是从小到大排序的!
         PriorityQueue pq = new PriorityQueue(k, new Comparator() {
             public int compare(Integer o1, Integer o2) {
                 if(o1 > o2) {
                     return 1;
                 } else if(o1 < o2) {
                     return -1;
                 } else {
                     return 0;
                 }
             }
         });

排序用priorityqueue有奇效,求第k个大的数,前K个大的数,用一个minheap

遍历HashMap:

for(Map.Entry entry : map.entrySet()) {
  getKey()
  getValue()
Collections.sort(result, Collections.reverseorder())

巧妙 需要学习

        for (Point p : points) {
            pq.add(p);
            if (pq.size() > k) {
                pq.poll();
            }
        }

!!!
降序
return o2 - 0o1

int compare(Object o1, Object o2) 返回一个基本类型的整型
如果要按照升序排序,
则o1 小于o2,返回-1(负数),相等返回0,01大于02返回1(正数)
如果要按照降序排序
则o1 小于o2,返回1(正数),相等返回0,01大于02返回-1(负数)

你可能感兴趣的:(8.12)