当前位置: 首页 > article >正文

数据结构与算法——Java实现 30.合并多个有序链表 小顶堆实现

后来我们都走了很久,远到提及往事时,

总会加上once upon a time

                                                —— 24.10.6

23. 合并 K 个升序链表

给你一个链表数组,每个链表都已经按升序排列。

请你将所有链表合并到一个升序链表中,返回合并后的链表。

示例 1:

输入:lists = [[1,4,5],[1,3,4],[2,6]]
输出:[1,1,2,3,4,4,5,6]
解释:链表数组如下:
[
  1->4->5,
  1->3->4,
  2->6
]
将它们合并到一个有序链表中得到。
1->1->2->3->4->4->5->6

示例 2:

输入:lists = []
输出:[]

示例 3:

输入:lists = [[]]
输出:[]

提示:

  • k == lists.length
  • 0 <= k <= 10^4
  • 0 <= lists[i].length <= 500
  • -10^4 <= lists[i][j] <= 10^4
  • lists[i] 按 升序 排列
  • lists[i].length 的总和不超过 10^4

思路

将k个升序链表依次遍历,因为他们升序,所以比较三个升序链表的第一个元素值,将三个元素中的最小值放入堆顶,然后被放入元素的那个链表的指针向后移动一位,直到k个升序链表中的所有元素都被进行比较移入堆中,由于是小顶堆,所以小的元素会移动在前,形成一个升序链表,最终得出合并后的升序链表

小顶堆实现

public class MinHeap {
    ListNode[] array;
    int size;

    public MinHeap(int capacity) {
        array = new ListNode[capacity];
    }

    public boolean offer(ListNode node) {
        if (isFull()){
            return false;
        }
        int child = size;
        size++;
        int parent = (child - 1) / 2;
        while (child >0 && node.val < array[parent].val) {
            array[child] = array[parent];
            child = parent;
            parent = (child - 1) / 2;
        }
        array[child] = node;
        return true;
    }

    public ListNode poll() {
        if (isEmpty()) {
            return null;
        }
        swap(0,size-1);
        size--;
        ListNode e = array[size];
        array[size] = null;

        // 下潜
        down(0);
        return e;
    }

    private void down(int parent) {
        int left = 2 * parent+1;
        int right = left + 1;
        // 假设父元素优先级最高
        int max = parent;
        if (left < size && array[left].val < array[max].val) {
            max = left;
        }
        if (right < size && array[right].val < array[max].val) {
            max = right;
        }
        // 有孩子优先级大于父节点
        if (max != parent) {
            swap(max,parent);
            down(max);
        }
    }

    private void swap(int i, int j) {
        ListNode temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public boolean isFull(){
        return size == array.length;
    }
}

 主函数

public class LeetCode23MergeMoreList {
    public ListNode mergeKLists(ListNode[] lists) {
        MinHeap heap = new MinHeap(lists.length);
        // 1.将链表的头结点加入小顶堆
        for (ListNode node : lists) {
            if (node != null) {
                heap.offer(node);
            }
        }
        // 2.不断从堆顶移除最小元素,加入新链表
        ListNode s = new ListNode(-1,null);
        ListNode cur = s;
        while (!heap.isEmpty()) {
            ListNode node = heap.poll();
            cur.next = node;
            cur = node;

            if (cur.next != null) {
                heap.offer(node.next);
            }
        }
        return s.next;
    }

    public static void main(String[] args) {
        ListNode[] lists = {
                ListNode.of(1,4,5),
                ListNode.of(2,3,6),
                ListNode.of(3,4,7),
        };
        ListNode m = new LeetCode23MergeMoreList().mergeKLists(lists);
        System.out.println(m);
    }
}

 

力扣

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */


class Solution {

    public ListNode mergeKLists(ListNode[] lists) {
        MinHeap heap = new MinHeap(lists.length);
        // 1.将链表的头结点加入小顶堆
        for (ListNode node : lists) {
            if (node != null) {
                heap.offer(node);
            }
        }
        // 2.不断从堆顶移除最小元素,加入新链表
        ListNode s = new ListNode(-1,null);
        ListNode cur = s;
        while (!heap.isEmpty()) {
            ListNode node = heap.poll();
            cur.next = node;
            cur = node;

            if (cur.next != null) {
                heap.offer(node.next);
            }
        }
        return s.next;
    }

    static class MinHeap {
        ListNode[] array;
        int size;

        public MinHeap(int capacity) {
            array = new ListNode[capacity];
        }

        public boolean offer(ListNode node) {
            if (isFull()){
                return false;
            }
            int child = size;
            size++;
            int parent = (child - 1) / 2;
            while (child >0 && node.val < array[parent].val) {
                array[child] = array[parent];
                child = parent;
                parent = (child - 1) / 2;
            }
            array[child] = node;
            return true;
        }

        public ListNode poll() {
            if (isEmpty()) {
                return null;
            }
            swap(0,size-1);
            size--;
            ListNode e = array[size];
            array[size] = null;

            // 下潜
            down(0);
            return e;
        }

        private void down(int parent) {
            int left = 2 * parent+1;
            int right = left + 1;
            // 假设父元素优先级最高
            int max = parent;
            if (left < size && array[left].val < array[max].val) {
                max = left;
            }
            if (right < size && array[right].val < array[max].val) {
                max = right;
            }
            // 有孩子优先级大于父节点
            if (max != parent) {
                swap(max,parent);
                down(max);
            }
        }

        private void swap(int i, int j) {
            ListNode temp = array[i];
            array[i] = array[j];
            array[j] = temp;
        }

        public boolean isEmpty() {
            return size == 0;
        }

        public boolean isFull(){
            return size == array.length;
        }
    }

}


http://www.kler.cn/news/335340.html

相关文章:

  • 微信朋友圈实况照片需要注意隐私
  • 独立站如何批量查收录,独立站批量查询收录的操作方法
  • MongoDB入门:安装及环境变量配置
  • (刷题记录5)盛最多水的容器
  • “衣依”服装销售平台:Spring Boot技术架构剖析
  • leetcode99 恢复二叉搜索树
  • 基于微信小程序的健康管理系统(源码+定制+文档)
  • Python FastApi 实现签名验证
  • 上传本地项目到GitHub远程仓库(极简洁操作版)
  • 【2024版本】Mac/Windows IDEA安装教程
  • QT系统学习篇(2)- Qt跨平台GUI原理机制
  • 【玩转 JS 函数式编程_008】3.1.2 JavaScript 函数式编程筑基之:箭头函数——一种更流行的写法
  • 第十一章 缓存之更新/穿透/雪崩/击穿
  • CSS面试真题 part1
  • Nginx深度解析与实战应用
  • MATLAB与Git集成:实现高效版本控制的实践指南
  • TypeScript:装饰器
  • 前端中常用的几种单位写法及其解释
  • 猴子吃桃-C语言
  • 小程序使用echarts视图层会悬浮在所有视图之上问题原因