Leetcode23-Merge k Sorted Lists

25 篇文章 0 订阅
22 篇文章 0 订阅

欢迎参观我的博客Huhaoyu’s Blog

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

分析

合并k个已排序的链表,并分析时间复杂度。k为可大可小的参数,表示需要合并的链表数目。
自定义一个Heap类:

  • vector<ListNode*> heap成员变量作为最小堆。
  • vector<ListNode*> indexHeap记录最小堆内各元素来自于lists中的那个链表。与最小堆heap内的元素一一对应。
  • Heap(vector<ListNode*>& lists);构造Heap对象,抽取lists各链表头部的第一个元素。
  • void push(ListNode*, int);heap中插入一个元素,对应地向indexHeap中插入其链表编号。
  • int pop();删除堆顶元素并调整堆,同时返回删除元素所在的链表编号,下一步可以在该链表中抽取下一个元素。
  • ListNode* front();返回堆顶元素(不删除)。
  • bool empty();返回堆是否为空。
  • adjustUp(int index);将堆中编号为index的元素向上调整。
  • adjustDown(int index);将堆中编号为index的元素向下调整。

时间复杂度:设第i个链表的长度为ni,则时间复杂度为O(sum(ni)*logk)。每从堆中取出一个节点,需要O(logk)调整堆,最坏情况总共需要sum(ni)次调整。

注意

  • lists中某链表已经完全插入到合并链表中时,应避免向堆中插入NULL
  • adjustDown中循环体应注意s != index,否则会进入死循环,详见代码。

AC代码

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */

class Heap {
public:
    Heap(vector<ListNode*>& lists) {
        for (int i = 0; i != lists.size(); ++i) {
            if (lists[i]) {
                push(lists[i], i);
                lists[i] = lists[i]->next;
            }
        }
        // for (int i = heap.size() - 1; i >= 0; --i) {
        //  adjustDown(i);
        // }
    }
    int size() {
        return heap.size();
    }
    bool empty() {
        return heap.empty() && indexHeap.empty();
    }
    void push(ListNode* pointer, int index) {
        heap.push_back(pointer);
        indexHeap.push_back(index);
        adjustUp(heap.size() - 1);
    }
    int pop() {
        int index = indexHeap.front();
        heap.front() = heap.back();
        indexHeap.front() = indexHeap.back();
        heap.pop_back();
        indexHeap.pop_back();
        adjustDown(0);
        return index;
    }
    ListNode* front() {
        return heap.front();
    }
private:
    vector<ListNode*> heap;
    vector<int> indexHeap;
    void adjustDown(int index) {
        if (heap.empty()) {
            return;
        }
        ListNode* save = heap[index];
        int saveIndex = indexHeap[index];
        for (int s = index * 2 + 1; s < heap.size(); s = s * 2 + 1) {
            if (s + 1 < heap.size() && heap[s]->val > heap[s+1]->val) {
                s += 1;
            }
            if (heap[s]->val < save->val) {
                heap[index] = heap[s];
                indexHeap[index] = indexHeap[s];
                index = s;
            } else {
                break;
            }
        }
        heap[index] = save;
        indexHeap[index] = saveIndex;
    }
    void adjustUp(int index) {
        ListNode* save = heap[index];
        int saveIndex = indexHeap[index];
        for (int s = (index - 1) / 2; s >= 0 && s != index; s = (s - 1) / 2) {
            if (heap[s]->val > save->val) {
                heap[index] = heap[s];
                indexHeap[index] = indexHeap[s];
                index = s;
            } else {
                break;
            }
        }
        heap[index] = save;
        indexHeap[index] = saveIndex;
    }
};

class Solution {
public:
    ListNode* mergeKLists(vector<ListNode*>& lists) {
        ListNode* root = NULL, *current;
        Heap heap(lists);
        while (!heap.empty()) {
            if (root) {
                current->next = heap.front();
                current = current->next;
            } else {
                root = current = heap.front();
            }
            int index = heap.pop();
            if (lists[index]) {
                heap.push(lists[index], index);
                lists[index] = lists[index]->next;
            }
        }
        return root;
    }
};

如代码或分析有误,请批评指正,谢谢。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值