Comparator lambda 积累

135 阅读1分钟
  • 在pq中存hashmap的key, poll的时候比较hashmap的val
  • #347
        Map<Integer, Integer> map = new HashMap<>();
        PriorityQueue<Integer> pq = new PriorityQueue<>((n1, n2) -> map.get(n1) - map.get(n2));
  • 二维数组按照每个数组的第一个元素升序排列
  • #56
Arrays.sort(intervals, (int[] a, int[] b) -> {
            return a[0] - b[0];
        });
  • 平面上的点距原点位置距离的排序(堆顶是最远的)
  • #973
int[][] res = new int[k][2];
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> b[0] * b[0] + b[1] * b[1] - a[0] * a[0] - a[1] * a[1]);