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

网站空间升级通知百度搜索量统计

网站空间升级通知,百度搜索量统计,网站建设 漳州,中山做网站排名23. 合并 K 个升序链表 - 力扣(LeetCode)https://leetcode.cn/problems/merge-k-sorted-lists/?envTypestudy-plan-v2&envIdtop-100-liked 顺序合并 合并两个有序链表作为子函数,创建一个空链表,然后对含有多个链表的数组进…

23. 合并 K 个升序链表 - 力扣(LeetCode)https://leetcode.cn/problems/merge-k-sorted-lists/?envType=study-plan-v2&envId=top-100-liked

顺序合并

合并两个有序链表作为子函数,创建一个空链表,然后对含有多个链表的数组进行遍历,不断合并空链表与当前链表得到新链表直到遍历完毕。

//c++
/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode() : val(0), next(nullptr) {}*     ListNode(int x) : val(x), next(nullptr) {}*     ListNode(int x, ListNode *next) : val(x), next(next) {}* };*/
class Solution {
public:ListNode* mtwo(ListNode* h1,ListNode* h2){if((!h1)||(!h2))    return h1?h1:h2;ListNode* ans=new ListNode(0),*pre=ans,*t1=h1,*t2=h2;while(t1&&t2){if(t1->val<t2->val){pre->next=t1;t1=t1->next;}else{pre->next=t2;t2=t2->next;}pre=pre->next;}pre->next=t1?t1:t2;return ans->next;}ListNode* mergeKLists(vector<ListNode*>& lists) {ListNode* ans=nullptr;for(int i=0;i<lists.size();i++)    ans=mtwo(ans,lists[i]);return ans;}
};#python
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:def mtwo(self, a:ListNode,b:ListNode)->ListNode:if not a or not b:return a if a else bh=t=ListNode()aa,bb=a,bwhile aa and bb:if aa.val>bb.val:t.next=bbbb=bb.nextelse:t.next=aaaa=aa.nextt=t.nextt.next=aa if aa else bbreturn h.nextdef mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]:ans=Nonefor i in lists:ans=self.mtwo(ans,i)return ans

分治合并

合并两个有序链表作为子函数,对含有多个链表的数组进行不断的二分递归,直到只含一个链表在回溯到上一个递归进行合并两个有序链表的操作直到回溯到对整个链表数组的合并。

//c++
/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode() : val(0), next(nullptr) {}*     ListNode(int x) : val(x), next(nullptr) {}*     ListNode(int x, ListNode *next) : val(x), next(next) {}* };*/
class Solution {
public:ListNode* mtwo(ListNode* h1,ListNode* h2){if((!h1)||(!h2))    return h1?h1:h2;ListNode* ans=new ListNode(0),*pre=ans,*t1=h1,*t2=h2;while(t1&&t2){if(t1->val<t2->val){pre->next=t1;t1=t1->next;}else{pre->next=t2;t2=t2->next;}pre=pre->next;}pre->next=t1?t1:t2;return ans->next;}ListNode* fen(vector <ListNode*> &a,int b,int c){if(b==c)    return a[b];if(b>c)    return nullptr;int m=(b+c)>>1;return mtwo(fen(a,b,m),fen(a,m+1,c));}ListNode* mergeKLists(vector<ListNode*>& lists) {return fen(lists,0,lists.size()-1);}
};#python
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:def mtwo(self, a:ListNode,b:ListNode)->ListNode:if not a or not b:return a if a else bh=t=ListNode()aa,bb=a,bwhile aa and bb:if aa.val>bb.val:t.next=bbbb=bb.nextelse:t.next=aaaa=aa.nextt=t.nextt.next=aa if aa else bbreturn h.nextdef fen(self,a:List[Optional[ListNode]],b:int,c:int):if b==c:return a[b]if b>c:return Nonem=(b+c)>>1return self.mtwo(self.fen(a,b,m),self.fen(a,m+1,c))def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]:return self.fen(lists,0,len(lists)-1)

优先队列合并

c++应用结构体创造最小堆的优先队列,python用库函数heapq。对多个链表的数组的多个链表的第一个元素进行堆的初始化,然后不断取堆顶,并将堆顶的下一个元素入堆,直到堆为空。

//c++
/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode() : val(0), next(nullptr) {}*     ListNode(int x) : val(x), next(nullptr) {}*     ListNode(int x, ListNode *next) : val(x), next(next) {}* };*/
class Solution {
public:struct a{int val;ListNode* aa;bool operator<(const a &others) const{   return val>others.val;    }};priority_queue <a> q;ListNode* mergeKLists(vector<ListNode*>& lists) {for(auto i:lists)   if(i)   q.push({i->val,i});ListNode ans,*pre=&ans;while(!q.empty()){auto i=q.top();q.pop();pre->next=i.aa;pre=pre->next;if(i.aa->next)  q.push({i.aa->next->val,i.aa->next});}return ans.next;}
};#python
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]:import heapqq=[]for i,j in enumerate(lists):if j:heapq.heappush(q,(j.val,i,j))ans=pre=ListNode(0)while q:i,j,k=heapq.heappop(q)pre.next=k pre=pre.nextif k.next:heapq.heappush(q,(k.next.val,j,k.next))return ans.next

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

相关文章:

  • 做个网站跳转链接怎么做aso排名优化知识
  • 前端只是做网站吗无屏蔽搜索引擎
  • 政府网站建设考试题目上海最新发布
  • 重庆工厂网站建设搜索引擎优化岗位
  • 怎么做网站教程+用的工具怎么免费搭建自己的网站
  • 自建站和独立站黑帽seo培训大神
  • 郑州市住房和城乡建设委员会网站seo接单平台有哪些
  • 重庆网站建设只选承越搜索引擎费用
  • 网站建设代理平台百度推广怎么优化排名
  • 广州网站建设oem百度推广400电话
  • WordPress文章数据表搜索引擎排名优化方案
  • 什么做自己的网站 应招聘人才百度快照如何优化
  • 期末作业做网站的心得体会南京怎样优化关键词排名
  • 网站建设 时间安排引流软件下载站
  • 建设厅业绩可查询网站地址网盟推广是什么意思
  • wordpress分页出现404企业seo的措施有哪些
  • dw网站建设框架大小设定免费b2b信息发布网站
  • 深圳网站建设外贸公司价格友链交换
  • 网站公安备案 北京今日头条网站推广
  • 做网站策划薪酬网店怎么运营和推广
  • 外销网站建立360指数在线查询
  • 深圳民治网站建设全网最低价24小时自助下单平台
  • 西安月子中心网站制作深圳网络推广怎么做
  • 高定网站株洲网站设计
  • 网站开发 项目计划seo排名哪家有名
  • 个人如何做网站软件百度指数查询移动版
  • 5g网络架构win10必做的优化
  • 哪家建站好网络营销品牌推广公司
  • 网站wap版怎么做自媒体十大平台
  • aspcms手机网站源码百度竞价网站