Leetcode - Merge k Sorted Lists

``
Screenshot from 2016-01-23 15:58:48.png

My code:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode mergeKLists(ListNode[] lists) {
        if (lists == null || lists.length == 0)
            return null;
        else if (lists.length == 1)
            return lists[0];
        return divide(lists, 0, lists.length - 1); 
    }
    
    private ListNode divide(ListNode[] lists, int begin, int end) {
        if (end - begin + 1 < 2)
            return lists[begin];
        int middle = (begin + end) / 2;
        ListNode left = divide(lists, begin, middle);
        ListNode right = divide(lists, middle + 1, end);
        return mergeTwoLists(left, right);
    }
    
    private ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if (l1 == null)
            return l2;
        else if (l2 == null)
            return l1;
        ListNode dummy = new ListNode(-1);
        ListNode temp = dummy;
        while (l1 != null && l2 != null) {
            if (l1.val < l2.val) {
                temp.next = l1;
                l1 = l1.next;
            }
            else {
                temp.next = l2;
                l2 = l2.next;
            }
            temp = temp.next;
        }
        temp.next = (l1 == null) ? l2 : l1;
        return dummy.next;
    }
}

我就是采用了merge sort 的思想,把這些lists先divide,到只剩下兩個(gè)的時(shí)候,再merge一下。算了下復(fù)雜度,其實(shí)挺高的。每個(gè)list都需要重復(fù)訪問多次把。
比如0, 1, 2, 3 條list
那么 0 ,1 merge, 2 和3 merge
然后 0,1組成的綜合體與2,3組成的綜合體再merge
這個(gè)時(shí)候又需要訪問下0,1,2,3。 有重復(fù)。時(shí)間復(fù)雜度應(yīng)該比較高。雖然看測試下來,效果還不錯(cuò)。

后來看了一個(gè)新的解法,復(fù)雜度要低很多。用priority queue 來做。
My code:

import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode mergeKLists(ListNode[] lists) {
        if (lists == null || lists.length == 0)
            return null;
        PriorityQueue<ListNode> pq = new PriorityQueue<ListNode>(lists.length, new Comparator<ListNode>() {
                public int compare(ListNode o1, ListNode o2) {
                    if (o1.val < o2.val)
                        return -1;
                    else if (o1.val == o2.val)
                        return 0;
                    else
                        return 1;
                }  
        });
        
        for (int i = 0; i < lists.length; i++) {
            if (lists[i] != null)
                pq.add(lists[i]);
        }
        
        ListNode dummy = new ListNode(-1);
        ListNode scanner = dummy;
        while (pq.size() > 0) {
            ListNode temp = pq.poll();
            scanner.next = temp;
            scanner = scanner.next;
            if (temp.next != null)
                pq.add(temp.next);
        }    
        return dummy.next;
    }
}

具體看下代碼就懂了。復(fù)雜度是 log(k) * n
k = number of lists
n = number of all nodes

注意下這道題目用到了 PrioirtyQueue 這個(gè)數(shù)據(jù)結(jié)構(gòu),并且可以自帶Comparator進(jìn)去。
記得上次使用到的數(shù)據(jù)結(jié)構(gòu), TreeSet, 其實(shí)就是Binary search tree

參考網(wǎng)站:
http://www.programcreek.com/2013/02/leetcode-merge-k-sorted-lists-java/

Anyway, Good luck, Richardo!

My code:

import java.util.PriorityQueue;
import java.util.Comparator;

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode mergeKLists(ListNode[] lists) {
        if (lists == null || lists.length == 0) {
            return null;
        }
        
        PriorityQueue<ListNode> pq = new PriorityQueue<ListNode>(8, new Comparator<ListNode>() {
            public int compare(ListNode o1, ListNode o2) {
                return o1.val - o2.val;
            }
        });
        
        for (int i = 0; i < lists.length; i++) {
            if (lists[i] != null) {
                pq.offer(lists[i]);
            }
        }
        
        ListNode dummy = new ListNode(-1);
        ListNode pre = dummy;
        while (!pq.isEmpty()) {
            ListNode curr = pq.poll();
            if (curr.next != null) {
                pq.offer(curr.next);
            }
            pre.next = curr;
            pre = curr;
        }
        
        return dummy.next;
    }
    
    public static void main(String[] args) {
        Solution test = new Solution();
        System.out.println("111");
    }
}

how to initialize a PriorityQueue object

PriorityQueue<Integer> pq = new PriorityQueue<Integer>(8, new Comparator<Integer>() {
    public int compare(Integer i1, Integer i2) {
        return i1.compareTo(i2);
    }
});

覺得用pq 來做這道題目是最好的。

Anyway, Good luck, Richardo! -- 09/14/2016

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容