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

做胎儿羊水鉴定网站镇江百度公司

做胎儿羊水鉴定网站,镇江百度公司,查询网站备案号,怎么建立网站赚钱一、链表的常见技巧总结 二、两数相加 . - 力扣(LeetCode) class Solution { public:ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {//利用t来存进位信息int t0;ListNode*newheadnew ListNode(0);//创建一个哨兵节点,方便尾插List…

一、链表的常见技巧总结

二、两数相加

. - 力扣(LeetCode)

class Solution {
public:ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {//利用t来存进位信息int t=0;ListNode*newhead=new ListNode(0);//创建一个哨兵节点,方便尾插ListNode*ptail=newhead;//ptail方便尾插ListNode* cur1=l1,*cur2=l2;while(cur1||cur2||t==1)//t==1防止后面有进位没加上{if(cur1)  {t+=cur1->val; cur1=cur1->next;}if(cur2)  {t+=cur2->val;cur2=cur2->next;}ptail->next=new ListNode(t%10);ptail=ptail->next;t/=10;}ListNode*ret=newhead->next;delete newhead;return ret;}
};

 三、两两交换链表中的节点

 四、重排链表

. - 力扣(LeetCode)

class Solution {
public:void reorderList(ListNode* head) {//方法1,利用一个数据结构将每个节点存起来,通过下标去访问//方法2, (1)利用快慢指针,找中点 (2) 拆开链表 从中点开始往后翻转 (3)进行合并成新链表if(head==nullptr||head->next==nullptr||head->next->next==nullptr) return;ListNode*mid=midnode(head);//找到中间节点//断开链表ListNode*l1=head;ListNode*l2=mid->next;mid->next=nullptr;//然后反转2l2=reverseList(l2);//合并链表mergeList(l1,l2);}ListNode*midnode(ListNode* head){ListNode*fast=head;ListNode*slow=head;while(fast->next!=nullptr&&fast->next->next!=nullptr)//确保后面两步能走{fast=fast->next->next;slow=slow->next;}return slow;//此时慢指针指向的就是最小的节点}ListNode* reverseList(ListNode* head){ListNode*p1=nullptr;ListNode*p2=head;ListNode*p3=head->next;//记录下一个要遍历的点while(p2){p2->next=p1;p1=p2;p2=p3;if(p3) p3=p3->next ;}return p1;}void mergeList(ListNode* l1, ListNode* l2){ListNode* temp1,*temp2;while(l1!=nullptr&&l2!=nullptr){temp1=l1->next;temp2=l2->next;l1->next=l2;l1=temp1;//回到原链表0l2->next=l1;l2=temp2;//回到原链表}}
};

五、合并k个升序链表

. - 力扣(LeetCode)

 优先级队列:

class Solution {
public://建小堆需要greaterstruct greater //构造一个仿函数{bool operator()(const ListNode*l1,const ListNode*l2){return l1->val>l2->val;}};ListNode* mergeKLists(vector<ListNode*>& lists) {//建立优先级队列(小堆),每次将堆顶元素插入进去,然后再删除堆顶元素,插入下个位置priority_queue<ListNode*,vector<ListNode*>,greater> heap;//建立一个小堆//入堆for(auto l:lists) if(l) heap.push(l);//因为有可能里面存的是一个空链表//开始合并k个有序链表ListNode*newnode=new ListNode(0);ListNode*ptail=newnode;//用于帮助我们进行尾插while(!heap.empty()){//进行尾插ListNode*it=heap.top();ptail->next=it;ptail=it;//去到下一个位置准备尾插//删除堆顶元素并将该节点的下一个节点入堆 ,为空就不入heap.pop();if(it->next) heap.push(it->next);}//此时全部的元素都插入完成了,返回最终的链表ListNode*ret=newnode->next;delete newnode;return ret;//时间复杂度o(n*k*logk)}
};

分治思想:

//策略,利用递归解决问题,结合归并排序,合并两个有序链表  (利用分治思想)
class Solution {
public:ListNode* mergeKLists(vector<ListNode*>& lists){int n=lists.size();return merge(lists,0,n-1);//让merge帮助我们完成整个区间的归并}ListNode* merge(vector<ListNode*>& lists,int left,int right){//首先,处理边界情况,如果不存在链表或者是只有一个链表,此时没有必要进行下去if(left>right) return nullptr;if(left==right) return lists[left];//让merge帮助我们归并左右区间int mid=(left+right)>>1;ListNode*l1=merge(lists,left,mid);ListNode*l2=merge(lists,mid+1,right);//然后开始进行合并两个有序链表return mergetwolist(l1,l2);}ListNode*mergetwolist(ListNode*l1,ListNode*l2){//考虑两个链表为空的情况if(l1==nullptr) return l2;if(l2==nullptr) return l1;//此时两个链表必然不为空,开始进行合并ListNode*newhead=new ListNode(0);//哨兵节点ListNode*ptail=newhead;//帮助我们进行尾插ListNode*cur1=l1,*cur2=l2;//两个指针分别指向两个链表while(cur1&&cur2)//当两个都不为空的时候{if(cur1->val<cur2->val) {//此时要尾插cur1ptail->next=cur1;ptail=cur1;//更新到下一个位置cur1=cur1->next;//继续去下一个节点遍历}else{ptail->next=cur2;ptail=cur2;//更新到下一个位置cur2=cur2->next;//继续去下一个节点遍历}}//可能有的链表没有遍历完if(cur1) ptail->next=cur1;if(cur2) ptail->next=cur2;//此时返回到目标的位置ListNode*ret=newhead->next;delete newhead;return ret;}
};

六、k个一组翻转链表

. - 力扣(LeetCode)

class Solution {
public:ListNode* reverseKGroup(ListNode* head, int k) {int n=0;//记录总数ListNode*cur=head;while(cur)//统计节点个数,并推测有多少组{cur=cur->next;++n;}n/=k;//看看一共需要几组ListNode*newhead=new ListNode(0);//创建一个哨兵节点ListNode*prev=newhead;//记住被头插的点cur=head;//从head开始进行头插//翻转n组,每组翻转k个for(int i=0;i<n;++i){ListNode*temp=cur;for(int j=0;j<k;++j){//用头插的逻辑ListNode*next=cur->next;;cur->next=prev->next;prev->next=cur;cur=next;//继续去链表的下一个点}prev=temp;//更新prev}//循环结束后,将后面的不需要逆序的部分接上prev->next=cur;ListNode*ret=newhead->next;delete newhead;return ret;}
};

七、旋转链表

. - 力扣(LeetCode)

思路1:截断再连接

class Solution {
public:ListNode* rotateRight(ListNode* head, int k) {//让链表成环(闭合成环),然后在指定位置断开if(head==nullptr||head->next==nullptr||k==0) return head;int count=1;//数节点数量ListNode*ptail=head;while(ptail->next!=nullptr) //找到尾节点,并统计节点数{ptail=ptail->next;++count;}int add=count-k%count;//看看具体是翻转几次if(add==count) return head;//避免不需要翻转的情况//截断重连ListNode*cur=head;while(--add) cur=cur->next; //找到被截断的位置ListNode*ret=cur->next;cur->next=nullptr;//断开cur=ret;while(cur->next!=nullptr) cur=cur->next;//找到尾节点cur->next=head;//连接return ret; }
};

思路2:链表成环,指定位置截断

class Solution {
public:ListNode* rotateRight(ListNode* head, int k) {//让链表成环,然后在指定位置断开if(head==nullptr||head->next==nullptr||k==0) return head;int count=1;//数节点数量ListNode*ptail=head;while(ptail->next!=nullptr) //找到尾节点,并统计节点数{ptail=ptail->next;++count;}int add=count-k%count;//看看具体是翻转几次ptail->next=head;//头尾相连while(add--) ptail=ptail->next;ListNode*ret=ptail->next;ptail->next=nullptr;return ret; }
};

思路3:逆置前n-k个,再逆置后k个,最后整体逆置

class Solution {
public:ListNode* rotateRight(ListNode* head, int k) {if(head==nullptr||head->next==nullptr||k==0) return head;//先逆置前n-k个,再逆置后k个,再整体逆置int count=1;//数节点数量ListNode*ptail=head;while(ptail->next!=nullptr) //找到尾节点,并统计节点数{ptail=ptail->next;++count;}int add=count-k%count;//看看具体是翻转几次if(add==count) return head;//开始找前n-k个节点ListNode*cur=head;while(--add) cur=cur->next;ListNode*l2=cur->next;//第二个链表cur->next=nullptr;//断开ListNode* l1=reverse(head);l2=reverse(l2);head->next=ptail;//连接起来return reverse(l1);//然后整体翻转}ListNode*reverse(ListNode* head){ //只有一个节点,没什么好逆置的if(head==nullptr||head->next==nullptr) return head;ListNode*p1=nullptr,*p2=head,*p3=head->next;while(p2){p2->next=p1;p1=p2;p2=p3;if(p3) p3=p3->next;}return p1;}
};

http://www.jinmujx.cn/news/115187.html

相关文章:

  • 昆明网络推广昆明网站建设昆明昆明如何做网站搜索引擎优化
  • 广告公司做网站的效果怎么样线上销售的方法和技巧
  • 开发网站的项目背景描述yande搜索引擎官网入口
  • 建设网站服务器选择seo文章生成器
  • 邢台疫情最新政策全国最好网络优化公司
  • 网站设计需求搜索引擎优化的方法有哪些?
  • 推荐网站在线看兄弟们seo是什么职业
  • 做标书有哪些网站能接到职业培训机构哪家最好
  • 请人做网站注意事项深圳seo优化外包
  • 关于公司网站怎么做谷歌浏览器免费入口
  • 大都会同行票怎么使用视频内存优化大师
  • 建设银行官方网站登录电脑版我想学做互联网怎么入手
  • 有什么做木工的网站西安关键词排名优化
  • 如何知道自己网站主机百度推广的五大优势
  • 个人 备案 经营性网站站长交流平台
  • 哪些网站使用vue做的网站首页seo关键词布局
  • 深圳网站建设 联雅网络杭州网站排名seo
  • jsp网站开发论文最近10个新闻
  • 做气球装饰可以上哪些网站手机如何制作网站
  • 中文做网站网络销售推广平台
  • wordpress外贸网站建设seo排名
  • 搜索引擎优化的技巧重庆百度推广优化
  • 单页营销式网站模板下载今日油价92汽油价格调整最新消息
  • 如果给公司做网站百度地图在线查询
  • 前端开发工程师招聘陕西seo排名
  • 个人帮忙做网站吗什么是搜索引擎竞价推广
  • 猪八戒网做网站如何付款网站建设seo优化培训
  • go 做视频网站宁波营销型网站建设优化建站
  • 学信网 的企业网站给你做认证百度竞价排名费用
  • 在网站上做教学直播平台多少钱企业官网seo