[LintCode] Merge K Sorted Lists [DC/Heap]

news/2024/7/2 21:42:16

Problem

Merge k sorted linked lists and return it as one sorted list.

Analyze and describe its complexity.

Example

Given lists:

[
  2->4->null,
  null,
  -1->null
],

return -1->2->4->null.

Note

分治做法中,merge()函数依然是将链表结点两两进行比较,然后在sort()函数中迭代merge两个二分后sort()的结果。PriorityQueue更为简洁。

Solution

Divide & Conquer

public class Solution {
    public ListNode mergeKLists(List<ListNode> lists) {  
        if (lists == null || lists.size() == 0) return null;
        return sort(lists, 0, lists.size()-1);
    }
    public ListNode sort(List<ListNode> lists, int start, int end) {
        if (start < end) {
            int mid = (start+end)/2;
            return merge(sort(lists, start, mid), sort(lists, mid+1, end));
        }
        return lists.get(start);
    }
    public ListNode merge(ListNode n1, ListNode n2) {
        ListNode head = new ListNode(0);
        ListNode cur = head;
        while (n1 != null && n2 != null) {
            if (n1.val < n2.val) {
                cur.next = n1;
                n1 = n1.next;
            }
            else {
                cur.next = n2;
                n2 = n2.next;
            }
            cur = cur.next;
        }
        if (n1 != null) cur.next = n1;
        else cur.next = n2;
        return head.next;
    }
}

Priority Queue

Edited: 2018.3

public class Solution {
    public ListNode mergeKLists(List<ListNode> lists) {  
        if (lists == null || lists.size() == 0) return null;
        ListNode dummy = new ListNode(0);
        ListNode head = dummy;
        PriorityQueue<ListNode> pq = new PriorityQueue<ListNode> (1, new Comparator<ListNode> (){
            public int compare(ListNode n1, ListNode n2) {
                return n1.val - n2.val;
            }
        });
        for (int i = 0; i < lists.size(); i++) {
            if (lists.get(i) != null) pq.offer(lists.get(i));
        }
        while (!pq.isEmpty()) {
            head.next = pq.poll();
            head = head.next;
            //put the rest ListNode back to pq
            if (head.next != null) pq.offer(head.next);
        }
        return dummy.next;
    }
}

PriorityQueue Java 8

public class Solution {
    public ListNode mergeKLists(ListNode[] lists) {
        if (lists == null || lists.length == 0) return null;
        PriorityQueue<ListNode> heap = new PriorityQueue<>((n1, n2) -> n1.val-n2.val);
        for (ListNode n: lists) {
            if (n != null) heap.offer(n);
        }
        ListNode head = new ListNode(0);
        ListNode cur = head;
        while (!heap.isEmpty()) {
            cur.next = heap.poll();
            cur = cur.next;
            if (cur.next != null) heap.offer(cur.next);
        }
        return head.next;
    }
}

http://www.niftyadmin.cn/n/1944138.html

相关文章

【SLAM14讲】02 视觉SLAM基本架构

一、传感器 1.1 安装位置分类 根据安装位置分为两类&#xff1a; 携带于机器人本体 上的传感器&#xff0c;比如激光传感器、相机、轮式编码器、惯性测量单元&#xff08;Inertial Measurement Unit, IMU&#xff09;等等&#xff0c;它们测到的通常都是一些间接的物理量而不…

前端的第二十五天(事件、其他方法)

前端的第二十五天&#xff08;事件、其他方法&#xff09; 一、事件 1.jQuery 事件注册 2.jQuery 事件处理 3.jQuery 事件对象 二、其他方法 1.jQuery 拷贝对象 2.多库共存 3.jQuery 插件 jQuery 插件库 jQuery 之家 GitHub 中文翻译网站

Java实验四 TCP客户端和服务器的应用

实验内容 1&#xff0e;掌握Socket程序的编写&#xff1b; 2&#xff0e;掌握密码技术的使用&#xff1b; 3&#xff0e;设计安全 4、对通信内容进行摘要计算并验证 实验步骤 1.信息安全传送&#xff1a; 发送方A——————>接收方B A加密时&#xff0c;用B的…

java经典50行_求一个50行左右的JAVA代码,最好每行带注释,谢谢啦

匿名用户1级2017-03-30 回答展开全部/*这个相当详细了.程序也不算太难.而且给老师看的时候效果比较好.因为有图形化界面,又实现一个比较实用的功能.老师会比较高兴的.建立一个文件名为Change.java就可以编译了*//** 这个程序实现输入身高算出标准体重,输入体重,算出身高的功能*…

前端的第二十六天(初识AJAX、客户端和服务器、网络相关概念、通信协议、服务器环境的安装、网站、PHP基础语法)

一、初识AJAX 1.url地址 展示网页 、提供数据 2.异步刷新 避免整个界面刷新&#xff0c;加载更快、省流量 3.同步刷新 不影响当前界面现有的操作 二、客户端与服务器 一般情况下服务器的硬件配置都要高一些&#xff0c;因为访问量大的话容易宕机。 像淘宝双11的时候&a…

在一个页面修改数据,并且ajax刷新数据列表的数据实现。

最近做layui框架发现用别人封装好的东西总是无法满足我想要的效果。于是决定自己重新写。 今天讲分页 我这里将数据增删改查分成了4个页面&#xff0c;各个页面之间相互关联。 1.add 添加页 2.ajaxlist 采用ajax方式分页的数据页 3.edit修改页 4.volist主页 主页信息volist <…

系统地图的用法

1, 引用框架 CoreGraphics.framework MapKit.framework CoreLocation.framework2 导入主头文件 iOS5之后不需要手动导入#import <CoreLocation/CoreLocation.h>#import <MapKit/MapKit.h>&#xff08;1&#xff09;MapKit &#xff1a;用于地图展示&#xf…

find / -name *.py | xargs grep domain | wc -l

http://world77.blog.51cto.com/414605/209125 http://blog.csdn.net/windone0109/article/details/2817792 查找目录&#xff1a;find /&#xff08;查找范围&#xff09; -name 查找关键字 -type d查找文件&#xff1a;find /&#xff08;查找范围&#xff09; -name 查找关键…