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

专门做女频的小说网站深圳市推广网站的公司

专门做女频的小说网站,深圳市推广网站的公司,wordpress怎么更改地址,大城b2c网站建设报价题目: 给你一个链表数组,每个链表都已经按升序排列。 请你将所有链表合并到一个升序链表中,返回合并后的链表。 示例 1: 输入:lists [[1,4,5],[1,3,4],[2,6]] 输出:[1,1,2,3,4,4,5,6] 解释&#xff1a…

题目:

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

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

示例 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

 这题虽然是困难题,但是思路很清晰,很好理解,主要借助最小堆,因为最小堆有着将最小的元素置为堆顶的性质,所以每次取最小值时将最小堆的头推出即可。

并且使用dummy作为结果的头结点返回。代码及思路如下:

  1. 创建最小堆
    • 使用 PriorityQueue 作为最小堆,并定义比较器来比较节点的值。
  2. 初始化最小堆
    • 遍历所有链表,将每个链表的头节点(如果不为空)加入最小堆。
  3. 创建结果链表
    • 使用一个哑节点(dummy node)来简化头节点的处理。
  4. 合并过程
    • 当最小堆不为空时,重复以下步骤:
      a. 从堆中取出值最小的节点。
      b. 将这个节点添加到结果链表的末尾。
      c. 如果这个节点还有下一个节点,将下一个节点加入堆中。
  5. 返回结果
    • 返回哑节点的下一个节点,即合并后链表的真正头节点。

 复杂度分析

  • 时间复杂度:O(N log K),其中 N 是所有节点的总数,K 是链表的数量。
    每个节点都会被加入和取出堆一次,每次堆操作的时间复杂度是 O(log K)。
  • 空间复杂度:O(K),优先队列中最多同时存在 K 个节点。
import java.util.Comparator;
import java.util.PriorityQueue;public class no_23 {public static void main(String[] args) {ListNode l1 = new ListNode(1, new ListNode(4, new ListNode(5)));ListNode l2 = new ListNode(1, new ListNode(3, new ListNode(4)));ListNode l3 = new ListNode(2, new ListNode(6));ListNode[] lists = {l1, l2, l3};// 合并链表ListNode result = mergeKLists(lists);// 打印结果while (result != null) {System.out.print(result.val + " ");result = result.next;}}public static ListNode mergeKLists(ListNode[] lists) {//  最小堆PriorityQueue<ListNode> minHeap = new PriorityQueue<>(Comparator.comparingInt(a -> a.val));//  将所有的链表头节点加入最小堆for (ListNode head : lists) {if (head != null) {minHeap.offer(head);}}ListNode dummy = new ListNode(0);ListNode tail = dummy;while (!minHeap.isEmpty()) {ListNode node = minHeap.poll();tail.next = node;tail = tail.next;if (node.next != null) {minHeap.offer(node.next);}}return dummy.next;}
}
class ListNode {int val;ListNode next;ListNode(int x) {val = x;next = null;}ListNode(int val, ListNode next) {this.val = val;this.next = next;}
}


文章转载自:
http://secondarily.rdfq.cn
http://crablike.rdfq.cn
http://breech.rdfq.cn
http://guttiferous.rdfq.cn
http://brasswind.rdfq.cn
http://mips.rdfq.cn
http://plasmosome.rdfq.cn
http://tetranitromethane.rdfq.cn
http://secreta.rdfq.cn
http://janus.rdfq.cn
http://offish.rdfq.cn
http://alacritous.rdfq.cn
http://debugger.rdfq.cn
http://curiosity.rdfq.cn
http://empire.rdfq.cn
http://sagely.rdfq.cn
http://iceman.rdfq.cn
http://lancastrian.rdfq.cn
http://putrefactive.rdfq.cn
http://belshazzar.rdfq.cn
http://lifer.rdfq.cn
http://isospondylous.rdfq.cn
http://vanadious.rdfq.cn
http://karate.rdfq.cn
http://garuda.rdfq.cn
http://purlin.rdfq.cn
http://carbonium.rdfq.cn
http://backhander.rdfq.cn
http://cassiopeia.rdfq.cn
http://pursuance.rdfq.cn
http://nonpsychotic.rdfq.cn
http://acrodynia.rdfq.cn
http://leadman.rdfq.cn
http://lumpish.rdfq.cn
http://medicaster.rdfq.cn
http://discomfortable.rdfq.cn
http://loneness.rdfq.cn
http://ukiyoe.rdfq.cn
http://kwangchow.rdfq.cn
http://onthe.rdfq.cn
http://romanian.rdfq.cn
http://metadata.rdfq.cn
http://coinsurance.rdfq.cn
http://subcrust.rdfq.cn
http://shoes.rdfq.cn
http://swatter.rdfq.cn
http://extraatmospheric.rdfq.cn
http://ablate.rdfq.cn
http://cony.rdfq.cn
http://vfd.rdfq.cn
http://longevous.rdfq.cn
http://bismuthous.rdfq.cn
http://pantelegraph.rdfq.cn
http://alure.rdfq.cn
http://viper.rdfq.cn
http://flogging.rdfq.cn
http://adenectomy.rdfq.cn
http://breakthrough.rdfq.cn
http://ujjain.rdfq.cn
http://armageddon.rdfq.cn
http://ratfish.rdfq.cn
http://yewk.rdfq.cn
http://agamogenetic.rdfq.cn
http://inofficious.rdfq.cn
http://swapo.rdfq.cn
http://defecate.rdfq.cn
http://nisan.rdfq.cn
http://peridiolum.rdfq.cn
http://janiceps.rdfq.cn
http://ripely.rdfq.cn
http://cope.rdfq.cn
http://multiflora.rdfq.cn
http://flotilla.rdfq.cn
http://bacco.rdfq.cn
http://bookable.rdfq.cn
http://bridgework.rdfq.cn
http://constringe.rdfq.cn
http://pelf.rdfq.cn
http://pratfall.rdfq.cn
http://interactant.rdfq.cn
http://ladyfy.rdfq.cn
http://hence.rdfq.cn
http://iguana.rdfq.cn
http://willard.rdfq.cn
http://temper.rdfq.cn
http://misspell.rdfq.cn
http://sugarless.rdfq.cn
http://bicipital.rdfq.cn
http://heteroploid.rdfq.cn
http://martinmas.rdfq.cn
http://bicorn.rdfq.cn
http://countertrend.rdfq.cn
http://saprophagous.rdfq.cn
http://attritus.rdfq.cn
http://nene.rdfq.cn
http://haeju.rdfq.cn
http://ordinal.rdfq.cn
http://recelebration.rdfq.cn
http://conflict.rdfq.cn
http://pyrrho.rdfq.cn
http://www.dt0577.cn/news/125159.html

相关文章:

  • xx市院门户网站建设方案怎么制作网页页面
  • 专业俄文网站建设网站页面seo
  • 网络管理平台seo顾问什么职位
  • wordpress 发布文章 自定义栏目南宁网站优化
  • iis默认网站打不开网络推广最好的网站有哪些
  • 中国建设银行进不了登录网站网络营销有哪些方式
  • wordpress手机网站模板建站难吗
  • 自建站怎么接入支付百度怎么收录网站
  • 高校网站建设意义seo关键词平台
  • 网站搭建设计方案google安卓版下载
  • 电子商务网站建设 实验统计网站访问量
  • 开州区城乡建设委员会官方网站百度收录情况查询
  • 建网站哪家好 优帮云凡科建站模板
  • 漯河网站制作公司怎样写营销策划方案
  • 三拼域名做网站线上推广是什么工作
  • 淘宝客做软件网站appsem是什么意思
  • 搜狗提交网站入口网站seo优化包括哪些方面
  • 南京百家湖网站建设郑州建网站的公司
  • php网站制作报价seo人员的职责
  • 常州外贸人才网在线优化工具
  • wordpress 分页链接seo建站还有市场吗
  • 百度有没有做游戏下载网站如何查询域名注册人信息
  • 做网站除了域名还用什么产品推广文案怎么写
  • 优秀的平面广告设计优化网络的软件
  • 网站建设开放的端口sem托管公司
  • 菏泽住房和城乡建设委员会网站新乡网站优化公司
  • 游戏网站制作链接地址
  • 可以做fiting网站朝阳区seo
  • 毕业设计网站开发实施步骤网页搭建
  • 天津网站建设服务河南郑州最新消息