Java Priority Queue Comparator overload 寫法

PriorityQueue Comparator: int compare(ListNode p, ListNode q) p.val < q.val,若 p 要排在前面,要回傳 -1,反之回傳 1。

範例:

PriorityQueue<ListNode> queue = new PriorityQueue<ListNode>(lists.length,new Comparator<ListNode>(){
    @Override
    public int compare(ListNode p, ListNode q){
        return p.val < q.val ? -1 : 1;
    }
});

sort list use comparator

Collections.sort(list, new Comparator<Interval>() {
            @Override
            public int compare(Interval a, Interval b) {
                return a.start < b.start ? -1 : 1;
            }
        });

results matching ""

    No results matching ""