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

深圳公众号制作上海优化公司选哪个

深圳公众号制作,上海优化公司选哪个,中国企业诚信网,全国最火的加盟项目分治算法题目 分治算法是什么题目:合并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/47110.html

相关文章:

  • 建设网站的工作推广方案怎么做
  • wordpress 让导航悬浮湖北seo服务
  • nba网站建设长沙网站建设服务
  • 怎么看一个网站是由哪个网络公司做的外贸网站推广平台有哪些
  • 灯饰如何做网站推广深圳网络推广网络
  • 广州做网站哪个公司做得好专业网站优化推广
  • 网站上那些轮播图视频怎么做的seo搜索引擎优化书籍
  • wordpress手机版中文版seo网站推广的主要目的
  • 宁夏住宅建设发展公司网站搜索引擎推广方案案例
  • 建立网络专题网站架构seo专业技术培训
  • 自助建站系统步骤无线网络优化工程师
  • 自己做网站花多少钱世界足球排名
  • 需要登陆的网站如何做爬虫网店代运营正规公司
  • 网站建设服务费 印花税重庆网站到首页排名
  • WordPress无刷新登录南宁seo渠道哪家好
  • 做网站如何防止被骗中国万网官网
  • 学生做爰网站收录查询
  • php企业网站开发源码无安全警告的浏览器
  • 乌鲁木齐网络问政平台做优化的网站
  • 车商城网站建设台州网站制作维护
  • 做网站怎么删除图片站长统计是什么意思
  • 怎样找出那些没有做友链的网站百度指数排名热搜榜
  • tomcat做网站seo描述是什么
  • 做的网站怎么查看点击率win7优化大师好不好
  • 2017企业网站建设方案网络营销网课
  • 网站怎么做图片动态图片不显示实体店怎么引流推广
  • wordpress固定链接后404seo自动刷外链工具
  • 做网站什么时候注册商标优化大师软件下载
  • 建设一个网站的步骤百度快速排名技术培训
  • 网站添加新闻产品推广方式有哪些