Leetcode23. Merge K sorted List

问题描述:

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.

Example:

Input:
[
  1->4->5,
  1->3->4,
  2->6
]
Output: 1->1->2->3->4->4->5->6

我的思路:

这道题是我在谷歌店面的时候遇见的,那个时候还没有刷到这个题,第一个想法是,把所有的list放到容器里,然后调用排序方法排序,所有的list放到容器里是N,然后排序方法使用快速排序为N+NlogN,最后取最大的应该是NlogN。

想的很美,但是我挂了。。后来看来别人的解题方法,估计也是面试官想要的解题方法,这道题应该用最小堆来解决。

在c++里,我们可以使用STL重的priority_queue,而在java中可使用PrioirtyQueue, 注意的是STL重的priority_queue默认为最大堆,所以我们要改变比较算法,因为ListNode为自定义类,我们需要重载比较方法(后面说)。

利用最小堆,首先先把list中所有的listnode放进最小堆中,然后每次取出头部并于返回的链表相连。

代码:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
struct cmp{
    bool operator() (ListNode *a, ListNode* b){
        return a->val > b->val;
    }
};


class Solution {
public:
    ListNode* mergeKLists(vector<ListNode*>& lists) {
        priority_queue<ListNode*, vector<ListNode*>, cmp > q;
        for(int i = 0; i < lists.size(); i++){
            if(lists[i])
                q.push(lists[i]);
        }
        ListNode *head = NULL, *pre = NULL, *tmp = NULL;
        while (!q.empty()) {
            tmp = q.top();
            q.pop();
            if (!pre) head = tmp;
            else pre->next = tmp;
            pre = tmp;
            if (tmp->next) q.push(tmp->next);
        }
        return head;
    }
};

我是借鉴了http://www.cnblogs.com/grandyang/p/4606710.html的第二种方法,由于对c++priority_queue不熟,我就直接复制了_(:з」∠)_

所以接下来我要记录一下关于priority_queue的相关内容

c++官方api:http://www.cplusplus.com/reference/queue/priority_queue/

声明:priority_queue<Type, Container, Functional>

其中type为数据类型,container为存储数据的容器,functional为比较数据的方法,一般默认为container为vector,而functional为<, 即为最大堆,优先输出大的元素。

对于内置数据类型,如int,想要改变输出的比较方式

可以通过 priority_queue<int, vector<int>, greater<int> > 来改变

对于自定义数据类型,可以重载操作符,即

struct cmp{
    bool operator() (ListNode *a, ListNode* b){
        return a->val > b->val;
    }
};

operator()重载函数需要被定义(声明)在一个新的结构体内

相关资料:

https://blog.csdn.net/xiaoquantouer/article/details/52015928

https://blog.csdn.net/woxiaohahaa/article/details/53191247

除此之外在discussion板块我还发现了另外的表达方式:

auto comp = [](const ListNode* l1, const ListNode* l2) {
            return l1->val > l2->val;
        };
        priority_queue < ListNode*, vector<ListNode*>, decltype(comp)> pq(comp);

这里使用了lambda表达式

详细见:如何在c++11中使用lambda表达式

原文地址:https://www.cnblogs.com/yaoyudadudu/p/9070218.html