[LeetCode] 23. Merge k Sorted Lists

news/2024/7/1 0:46:44

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

Heap

Time Complexity:
Update the heap costs O(nklogk)

Space Complexity:
O(kn)
The result listnode costs O(kn) and the heap is always O(k)

思路

Step1: Create a min heap with size k, loop through the input array of listnode and put all headnode into the heap
Step2: Create a new listnode two store the sorted list
Step3: Do the following steps k*n times (total number of the listnode)
(1) Pop out the min of the heap, add it to the result listnode
(2) If this listnode has next, insert it into the heap and update the heap

*这题中的input中,k个listnode还有其中个别的等于Null的情况,所以要判断一下再加入minheap

代码

public ListNode mergeKLists(ListNode[] lists) {
    if(lists == null || lists.length == 0) return null;
    ListNode dummy = new ListNode(0);
    ListNode head = dummy;
    PriorityQueue<ListNode> minHeap = new PriorityQueue<>(new Comparator<ListNode>(){
        public int compare(ListNode l1, ListNode l2){
            return l1.val - l2.val;
        }
    });
    
    for(int i = 0; i < lists.length; i++){
        if(lists[i] != null) minHeap.offer(lists[i]);
    }
    
    while(!minHeap.isEmpty()){
        ListNode min = minHeap.poll();
        head.next = min;
        head = head.next;
        if(min.next != null){
            minHeap.offer(min.next);
            min = min.next;
        }
    }
    return dummy.next;
}

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

相关文章

DataTable学习笔记2

虽然大多数时候您与DataTables的Javascript交互将使用本网站的“ 使用”部分中所述的初始化对象来完成&#xff0c;但有时您可能会发现对表进行一些外部控制很有用。jQuery.dataTable对象提供以下函数。 还有许多可用的插件API函数&#xff0c;它们扩展了DataTables的功能&…

前端 css+js实现返回顶部功能

描述&#xff1a; 本文主要是讲&#xff0c;通过cssjs实现网页中的【返回顶部】功能。 实现代码&#xff1a; HTML&#xff1a; 1 <div> 2 <button οnclick"returnTop()" id"btnTop" title"返回顶部">返回顶部</button> …

访问iframe页面session过期后跳转到登录页的问题

在MVC中一般都会封装BaseControl 来限制 是否有权限&#xff0c;登录 public class BaseController : Controller{public system_user LoginUser{ get; set; }protected override void OnActionExecuting(ActionExecutingContext filterContext){bool IsExt false;object obj …

SQL执行错误 错误消息:SQL Server检测到基于一致性的逻辑I/O错误

--1.设置数据库为单用户模式(会立即断开其他所有用户的连接) ALTER DATABASE YourDatabaseName SET SINGLE_USER WITH ROLLBACK IMMEDIATE; GO--2.修复数据库(允许数据丢失) DBCC CHECKDB (YourDatabaseName, REPAIR_ALLOW_DATA_LOSS)--3.单用户模式关闭(多用户模式开启) ALTER…

Windows 系统 安装SCons

1. 安装python2.7或以上版本&#xff0c;执行python2.x的安装包程序python-2.7.12.amd64.msi进行安装即可 2. 安装scons 下载scons-3.0.4.zip压缩包并解压缩&#xff0c;&#xff08;http://sourceforge.net/projects/scons/files/scons/2.3.2/&#xff09; CMD下进入解压后的…

window下 vs2017-- boost 编译及使用

boost的编译和使用&#xff0c;经过搜集资料和总结&#xff0c;记录成文。感谢文后所列参考资料的作者。 1 下载 地址&#xff1a;http://sourceforge.net/projects/boost/files/boost/1.56.0/ 可以选择 boost_1_56_0.7z 下载。 2 编译 2.1 生成boost的自用的编译工具bjam…

Microsoft Word 2016 VBA开发 通过脚本实现表格内显示、隐藏一行注释

背景 曾经的同事微信求助&#xff0c;在一个类似问卷的docx文件中的某个Table内&#xff0c;分别放入两个按钮&#xff1a;显示、隐藏&#xff0c;用于指导用户进行表格填写 方法 1. 创建一个docm文件&#xff0c;并启用宏 2. 在Visual Basic编辑器中插入如下代码 Public Sub S…

手机号归属地 查询

1、http://www.guisd.com 目前测试 有些号段无法查询出结构 2、http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel*** 3、https://www.juhe.cn/docs/api/id/72 需注册 免费 1000次/天 4、http://www.114best.com/dh/114.aspx?w百事通