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

金阊seo网站优化软件网站开发jsp

金阊seo网站优化软件,网站开发jsp,高端品牌型 营销型网站建设,中国空间站的意义LeetCode热题100(三十四) —— 23.合并K个升序链表 题目描述代码实现思路一:选择排序(199ms)思路二:归并排序(2ms) 思路解析 你好,我是杨十一,一名热爱健身的程序员在Coding的征程中,不断探索与…

LeetCode热题100(三十四) —— 23.合并K个升序链表

  • 题目描述
  • 代码实现
    • 思路一:选择排序(199ms)
    • 思路二:归并排序(2ms)
  • 思路解析

  • 你好,我是杨十一,一名热爱健身的程序员
  • 在Coding的征程中,不断探索与成长
  • LeetCode热题100——刷题记录(不定期更新)
    此系列文章用于记录我在学习 LeetCode热题100 过程中的总结和收获
    愿与诸君共同探讨,在代码世界里携手共进,攻克难题,提升自我

题目描述

给你一个链表数组,每个链表都已经按升序排列。
请你将所有链表合并到一个升序链表中,返回合并后的链表。
示例 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.length0 <= k <= 10^40 <= lists[i].length <= 500-10^4 <= lists[i][j] <= 10^4lists[i] 按 升序 排列lists[i].length 的总和不超过 10^4

代码实现

思路一:选择排序(199ms)

class Solution {public ListNode mergeKLists(ListNode[] lists) {ListNode newHead = new ListNode();ListNode newTail = newHead;while (true) {int index = -1;int minVal = Integer.MAX_VALUE;for (int i = 0; i < lists.length; i++) {if (lists[i] != null && lists[i].val < minVal) {index = i;minVal = lists[i].val;}}if (index == -1) break;newTail .next = lists[index];newTail = newTail .next;lists[index] = lists[index].next;}return newHead.next;}
}

思路二:归并排序(2ms)

class Solution {public ListNode mergeKLists(ListNode[] lists) {if (lists.length == 0) return null;if (lists.length == 1) return lists[0];int currentLength = lists.length;while (currentLength != 1) {int i = 0;while (i < currentLength / 2) {lists[i] = merge(lists[i * 2], lists[i * 2 + 1]);if (i > 0) {lists[i * 2] = null;lists[i * 2 + 1] = null;}i++;}if (currentLength % 2 == 1) {lists[i] = lists[currentLength - 1];currentLength = currentLength / 2 + 1;} else {currentLength = currentLength / 2;}}return lists[0];}public ListNode merge(ListNode nodeA, ListNode nodeB) {ListNode newHead = new ListNode();ListNode newTail = newHead;while (nodeA != null && nodeB != null) {if (nodeA.val < nodeB.val) {newTail.next = nodeA;nodeA = nodeA.next;} else {newTail.next = nodeB;nodeB = nodeB.next;}newTail = newTail.next;}if (nodeA == null) newTail.next = nodeB;if (nodeB == null) newTail.next = nodeA;return newHead.next;}
}
  • 数据结构
/** 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; }
}

思路解析

  1. 输入:链表头节点的数组ListNode[] lists
  2. 输出:合并后的有序链表头节点ListNode newHead
  3. 思路一:选择排序
    • 遍历所有链表的头节点,将其中最小的添加至有序的newHead链表中
    • 被选中节点的下一个节点作为该链表新的头节点

在这里插入图片描述

  1. 思路二:归并排序
    • 两两链表进行合并,参考LeetCode热题100(二十七)链表 —— 合并两个有序链表
    • 将合并后的结果保存在原数组中,需注意:
      • 合并过程中当链表数量为奇数时,最后单个的链表添加至数组末尾

在这里插入图片描述


  • 你好,我是杨十一,一名热爱健身的程序员
  • 在Coding的征程中,不断探索与成长
  • LeetCode热题100——刷题记录(不定期更新)
    此系列文章用于记录我在学习 LeetCode热题100 过程中的总结和收获
    愿与诸君共同探讨,在代码世界里携手共进,攻克难题,提升自我
http://www.yayakq.cn/news/829598/

相关文章:

  • 网站ico怎么用网站开发综合技能实训心得体会
  • 代替做网站推广2003 iis wordpress
  • 明星网页网站制作本地门户网站系统
  • 12306铁路网站开发语言遵义花果园网站建设
  • 阿里云网站建设流程教案网页设计和网站开发
  • 在线网页制作网站织梦做的的网站首页显示空白
  • 沧州网站开发建网站价格 建设网站需要多少钱
  • 班级的网站设计怎么做天津市建筑信息网
  • 课程网站建设技术服务公司网站建设
  • 做音乐 交流网站泰安人力资源招聘
  • 一个具体网站的seo优化上海h5网站建设
  • 成都网站优化推广方案wordpress需要安装哪些插件
  • 网站后台怎么做qq群自动加网站开发 加密保护
  • 怎样修改网站的主页内容建设银行大连招聘网站
  • 微信怎么建设自己网站wordpress博客登陆
  • 长沙网站seo服务wordpress列表分页代码
  • 网站开发外贸客户判断网站到期
  • 管理有限公司网站设计网页设计与网站建设课程
  • 网站建站中关键字搜索怎么弄哪个网站ppt模板免费下载
  • 有名的网站制作公司做阿里巴巴类似的网站吗
  • 小网站关键词搜什么wordpress导航标签文件在哪
  • 如何快速提升网站权重wordpress博客模板下载
  • 罗湖网站开发秘密入口3秒自动进入
  • 网站打不开 ...深圳公司网站建设设计
  • 免费自动建站站长工具是做什么的
  • 郑州做网站推广电话易优cms收费吗
  • 辛集市建设局网站商城建站流程
  • .net 网站 语言新公司起名大全集
  • 济南网站制作培训班工业设计就业前景
  • 做旅游网站需要引进哪些技术人才建设科技处网站