zoukankan      html  css  js  c++  java
  • [LeetCode] Merge k Sorted Lists

     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

    归并K个有序链表

    思路1:使用multiset存储每一个出现的元素。再将这些元素连接起来即可(vector也可以)

    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    public:
        ListNode* mergeKLists(vector<ListNode*>& lists) {
            multiset<int> mset;
            for (auto& list : lists)
            {
                ListNode* cur = list;
                while (cur)
                {
                    mset.insert(cur->val);
                    cur = cur->next;
                }
            }
            ListNode* head = nullptr;
            ListNode* node = nullptr;
            for (auto it = mset.begin(); it != mset.end(); ++it)
            {
                if (node)
                {
                    node->next = new ListNode(*it);
                    node = node->next;
                }
                else
                {
                    node = new ListNode(*it);
                    head = node;
                }
            }
            return head;
        }
    };

    思路2:堆排序,使用优先队列

    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    public:
        ListNode* mergeKLists(vector<ListNode*>& lists) {
            auto cmp = [](const ListNode* a, const ListNode* b) { return a->val > b->val; };
            priority_queue<ListNode*, vector<ListNode*>, decltype(cmp)> min_heap(cmp);
            for (auto& list : lists)
                if (list)
                    min_heap.push(list);
            ListNode* dummy = new ListNode(-1);
            ListNode* cur = dummy;
            while (!min_heap.empty())
            {
                ListNode* node = min_heap.top();
                min_heap.pop();
                cur->next = node;
                cur = cur->next;
                if (node->next)
                    min_heap.push(node->next);
            }
            return dummy->next;
        }
    };

    思路三:利用分治+递归

    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    public:
        ListNode* mergeKLists(vector<ListNode*>& lists) {
            if (lists.empty())
                return NULL;
            while (lists.size() > 1)
            {
                lists.push_back(mergeTwoLists(lists[0], lists[1]));
                lists.erase(lists.begin());
                lists.erase(lists.begin());
            }
            return lists[0];
        }
        
        ListNode* mergeTwoLists(ListNode* l1, ListNode* l2)
        {
            if (l1 == NULL)
                return l2;
            if (l2 == NULL)
                return l1;
            if (l1->val < l2->val)
            {
                l1->next = mergeTwoLists(l1->next, l2);
                return l1;
            }
            else
            {
                l2->next = mergeTwoLists(l1, l2->next);
                return l2;
            }
        }
    };
  • 相关阅读:
    【多线程】-实现多线程的三种方法
    在java项目启动时就执行某操作
    PHP上传多个Excel表格里的数据到数据库然后在页面显示
    PHP如何生成word并下载
    PHP把网页表单导出到word
    把PHP网页表单导出到word文档中
    HTTP Keep-Alive的作用
    利用paramiko获取上传下载远程服务器的资源信息
    3.django连接mysql数据库及安装mysqldb驱动报错解决办法
    3.Pycharm和navicate的使用
  • 原文地址:https://www.cnblogs.com/immjc/p/9067897.html
Copyright © 2011-2022 走看看