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

汉阳放心的建站企丿化妆品软文推广范文

汉阳放心的建站企丿,化妆品软文推广范文,资阳aso优化招聘,东莞最新招聘分治算法题目 分治算法是什么题目:合并K个升序链表总结 分治算法是什么 把问题分解后进行求解,相比于不分解直接求解,时间复杂度更低。符合这个特征的算法,我们才称之为「分治算法」。 题目:合并K个升序链表 题目 分…

分治算法+题目

  • 分治算法是什么
  • 题目:合并K个升序链表
  • 总结

分治算法是什么

问题分解后进行求解,相比于不分解直接求解,时间复杂度更低。符合这个特征的算法,我们才称之为「分治算法」。

题目:合并K个升序链表

题目
在这里插入图片描述
分治算法:

/*** 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 {//分治的思想 二分分治 时间复杂度是O(N*logN)public ListNode mergeKLists(ListNode[] lists) {int sz=lists.length;return merge(lists,0,sz-1);}//定义:合并lists[startIndex...endIndex]上的链表ListNode merge(ListNode[] lists,int startIndex,int endIndex){//没有链表if(startIndex>endIndex){return null;}//一条链表if(endIndex==startIndex){return lists[startIndex];}//分成两段 [startIndex,mid][mid+1,endIndex]int mid=(startIndex+endIndex)/2;//合并左边的ListNode left= merge(lists,startIndex,mid);//合并右边的ListNode right= merge(lists,mid+1,endIndex);//合并左右边的return mergeTwoLists(left,right);}ListNode mergeTwoLists(ListNode list1,ListNode list2){ListNode dummy=new ListNode(-1);ListNode cur=dummy;ListNode p1=list1;ListNode p2=list2;while(p1!=null && p2!=null){if(p1.val<p2.val){cur.next=p1;p1=p1.next;}else{cur.next=p2;p2=p2.next;}cur=cur.next;}while(p1!=null ){cur.next=p1;p1=p1.next;cur=cur.next;}while(p2!=null ){cur.next=p2;p2=p2.next;cur=cur.next;}cur.next=null;return dummy.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; }* }*/
class Solution {//把所有的节点放入 优先级队列中 然后一个个取出public ListNode mergeKLists(ListNode[] lists) {// 优先级队列,最小堆PriorityQueue<ListNode> pq = new PriorityQueue<>(new Comparator<ListNode>() {@Overridepublic int compare(ListNode o1, ListNode o2) {// TODO Auto-generated method stubreturn o1.val-o2.val;}});//取出所有链表 将每个节点入队列for(ListNode list:lists) {ListNode cur=list;while(cur!=null) {pq.add(cur);cur=cur.next;}}//取出所有节点ListNode dummy=new ListNode(-1),cur=dummy;while(!pq.isEmpty()) {cur.next=pq.poll();cur=cur.next;}cur.next=null;return dummy.next;}
}

总结

在这里插入图片描述

把递归算法抽象成递归树,如果递归树节点的时间复杂度和树的深度相关,那么使用分治思想对问题进行二分,就可以使递归树尽可能平衡,进而优化总的时间复杂度

http://www.dtcms.com/wzjs/375553.html

相关文章:

  • 网站系统功能描述优秀营销软文100篇
  • 网站建设中的图片缅甸在线今日新闻
  • 建设网站远达护肤品软文推广
  • 网站的原型图谷歌浏览器app
  • 齐全的赣州网站建设网络营销的整体概念
  • 益阳做网站怎么便宜国内网络推广渠道
  • 网站如何开启gzip压缩电商网站排名
  • 智能网站系统可以做app吗seo系统
  • 网站开发比较厉害搜索引擎有哪些
  • 网站建设广告背景图关键词如何快速排名
  • 欧美建设网站如何推广app更高效
  • 安徽疫情最新通报宁波seo教程
  • 英迈思做的网站怎么样设计网站的软件
  • 南阳建网站公司服务推广软文范例
  • 100个无水印短视频素材免费seo网站优化流程
  • 企业网站建设动图百度网页收录
  • 地产股最新消息外包seo公司
  • 网站制作 深圳信科网络武汉网络推广网络营销
  • 网站建设的征求意见稿东莞网络推广营销公司
  • 教做衣服网站chrome网页版入口
  • 怎样用flash做游戏下载网站nba最新资讯
  • 大一html5网页设计代码资源网站优化排名优化
  • 有口碑的赣州网站建设常州seo收费
  • 做网站网页多少钱怎么接游戏推广的业务
  • 用flash做的经典网站企业管理培训机构
  • 建设网站图片素材搜索引擎优化是做什么的
  • 大型公司为什么做网站网站推广方式有哪些
  • 广州电商网站建设如何推广公众号
  • 临安区做网站的公司百度排名点击器
  • 用discuz做门户网站上海百网优seo优化公司