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

适合做网站的图片山东seo百度推广

适合做网站的图片,山东seo百度推广,网站建设测试与维护,邵阳网站建设的话术leetcode-61 给你一个链表的头节点 head ,旋转链表,将链表每个节点向右移动 k 个位置。 示例 1: 输入:head [1,2,3,4,5], k 2 输出:[4,5,1,2,3]示例 2: 输入:head [0,1,2], k 4 输出&#x…

leetcode-61

给你一个链表的头节点 head ,旋转链表,将链表每个节点向右移动 k 个位置。

示例 1:

输入:head = [1,2,3,4,5], k = 2
输出:[4,5,1,2,3]

示例 2:

输入:head = [0,1,2], k = 4
输出:[2,0,1]

提示:

  • 链表中节点的数目在范围 [0, 500] 内
  • -100 <= Node.val <= 100
  • 0 <= k <= 2 * 109

思路:看图片可以发现,这个题目本质上,是把链表分成两部分,交换前后两部分的顺序

所以重点在于找到分裂点,以及拼接

/*** 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* rotateRight(ListNode* head, int k) {if(!head || !head->next || k == 0) {return head;}auto tmp = head;int count = 0;auto tail = head;while(tmp) {tail = tmp;tmp = tmp->next;++count;}tmp = head;k %= count;if(k == 0) {return head;}k = count - k;while(--k && tmp) {tmp = tmp->next;}auto new_head = tmp->next;tmp->next = nullptr;tail->next = head;return new_head;}   
};

leetcode-206

给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。

示例 1:

输入:head = [1,2,3,4,5]
输出:[5,4,3,2,1]

示例 2:

输入:head = [1,2]
输出:[2,1]

示例 3:

输入:head = []
输出:[]

提示:

  • 链表中节点的数目范围是 [0, 5000]
  • -5000 <= Node.val <= 5000

进阶:链表可以选用迭代或递归方式完成反转。你能否用两种方法解决这道题?

思路:递归法比较简单,还是我们的老模板

void circle(ListNode *head) {if(...) {return }        //边界条件auto res = circle(head->next);//other code//return
}

迭代法也是一器破万法。

关于链表反转的题目,都分成两部分,pre和curr

pre就是已经处理好的部分的尾节点, curr就是你要处理的部分的头节点

所以伪代码如下

//关于翻转链表相关的题目
while(xxx) {process(curr);      //实现翻转的逻辑pre = curr;         //当前部分已经处过,所以pre变成currcurr = curr->next;  //curr->next还没处理过,所以curr指向自己的next继续处理
}
/*** 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* reverseList(ListNode* head) {if(!head || !head->next) {return head;}// auto next = reverseList(head->next);// auto tmp = head->next;// head->next = nullptr;// tmp->next = head;// return next;auto pre = head;pre = nullptr;while(head) {auto next = head->next;head->next = nullptr;head->next = pre;pre = head;head = next;}return pre;}
};

leetcode-92

给你单链表的头指针 head 和两个整数 left 和 right ,其中 left <= right 。请你反转从位置 left 到位置 right 的链表节点,返回 反转后的链表 。

示例 1:

输入:head = [1,2,3,4,5], left = 2, right = 4
输出:[1,4,3,2,5]

示例 2:

输入:head = [5], left = 1, right = 1
输出:[5]

提示:

  • 链表中节点数目为 n
  • 1 <= n <= 500
  • -500 <= Node.val <= 500
  • 1 <= left <= right <= n

进阶: 你可以使用一趟扫描完成反转吗?

思路,和上题一样,不过是对翻转的区间有限制了。

加上前缀节点pre,找到要翻转的区间的头尾节点,将之翻转后。拼接回原来的链表

/*** 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* reverseBetween(ListNode* head, int left, int right) {if(!head || !head->next) {return head;}auto tmp = new ListNode(-1);tmp->next = head;auto pre = tmp;int count = left;while(--count) {tmp = tmp->next;}auto curr = tmp->next;for(int i=0; i<right-left; i++) {auto tail = curr->next->next;auto next = curr->next;curr->next = tail;auto new_tail = tmp->next;tmp->next = next;next->next = new_tail;}return pre->next;}
};

leetcode-24

给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换)。

示例 1:

输入:head = [1,2,3,4]
输出:[2,1,4,3]

示例 2:

输入:head = []
输出:[]

示例 3:

输入:head = [1]
输出:[1]

提示:

  • 链表中节点的数目在范围 [0, 100] 内
  • 0 <= Node.val <= 100

思路:还记得我们一器破万法的迭代法模板吗,直接套用就可以

/*** 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* swapPairs(ListNode* head) {if(!head || !head->next) {return head;}auto pre = new ListNode(-1);pre->next = head;auto curr = head;auto tmp = pre;while(curr && curr->next) {auto tail = curr->next->next;auto next = curr->next;curr->next = tail;auto new_tail = tmp->next;next->next = new_tail;tmp->next = next;tmp = curr;curr = curr->next;}return pre->next;}
};

leetcode-25

给你链表的头节点 head ,每 k 个节点一组进行翻转,请你返回修改后的链表。

k 是一个正整数,它的值小于或等于链表的长度。如果节点总数不是 k 的整数倍,那么请将最后剩余的节点保持原有顺序。

你不能只是单纯的改变节点内部的值,而是需要实际进行节点交换。

示例 1:

输入:head = [1,2,3,4,5], k = 2
输出:[2,1,4,3,5]

示例 2:

输入:head = [1,2,3,4,5], k = 3
输出:[3,2,1,4,5]

提示:
  • 链表中的节点数目为 n
  • 1 <= k <= n <= 5000
  • 0 <= Node.val <= 1000

进阶:你可以设计一个只用 O(1) 额外内存空间的算法解决此问题吗?

思路:做了以上几道题目,总结了自己的一器破万法模板之后,发现这个题竟然这么简单

以前被自己视为面试杀手的接雨水,k个一组翻转链表也变得简单起来

这题和上一题的唯一不同就在于,需要遍历一遍算出链表的长度,避免越界

/*** 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* reverseKGroup(ListNode* head, int k) {auto tmp = head;int count=0;while(tmp) {++count;tmp = tmp->next;}if(!head || !head->next || k>count || k<=1) {return head;}auto pre = new ListNode(-1);pre->next = head;tmp = pre;auto curr = head;    for(int i=0; i<count/k; i++) {for(int j=1; j<k; j++) {auto tail = curr->next->next;auto next = curr->next;curr->next = tail;auto new_tail = tmp->next;next->next = new_tail;tmp->next = next;}tmp = curr;curr = curr->next;}return pre->next;}
};


文章转载自:
http://dinncoescheatage.zfyr.cn
http://dinncounion.zfyr.cn
http://dinncodurable.zfyr.cn
http://dinncorodomontade.zfyr.cn
http://dinncobionics.zfyr.cn
http://dinncodeintegro.zfyr.cn
http://dinnconammet.zfyr.cn
http://dinnconatriuretic.zfyr.cn
http://dinncobiometrician.zfyr.cn
http://dinncouptown.zfyr.cn
http://dinncorhinopharyngocele.zfyr.cn
http://dinncopyroligneous.zfyr.cn
http://dinncopriestlike.zfyr.cn
http://dinncoambiguous.zfyr.cn
http://dinncosteamboat.zfyr.cn
http://dinncohurl.zfyr.cn
http://dinncolatania.zfyr.cn
http://dinncomenostaxis.zfyr.cn
http://dinncoisochromatic.zfyr.cn
http://dinncomanifestative.zfyr.cn
http://dinncoappendix.zfyr.cn
http://dinncobicipital.zfyr.cn
http://dinncoknow.zfyr.cn
http://dinncotessular.zfyr.cn
http://dinncohag.zfyr.cn
http://dinncomilden.zfyr.cn
http://dinncorigorism.zfyr.cn
http://dinncotankerman.zfyr.cn
http://dinncotriumvir.zfyr.cn
http://dinncolethargize.zfyr.cn
http://dinncovirosis.zfyr.cn
http://dinncothulium.zfyr.cn
http://dinncoscoresheet.zfyr.cn
http://dinncoparachute.zfyr.cn
http://dinncocontadina.zfyr.cn
http://dinncotrijugous.zfyr.cn
http://dinncohyoscine.zfyr.cn
http://dinncoabram.zfyr.cn
http://dinncoupbraid.zfyr.cn
http://dinncorotterdam.zfyr.cn
http://dinncorse.zfyr.cn
http://dinncooptimistically.zfyr.cn
http://dinncomassage.zfyr.cn
http://dinncobenthoal.zfyr.cn
http://dinncowaldensian.zfyr.cn
http://dinncocryptosystem.zfyr.cn
http://dinncounselfishly.zfyr.cn
http://dinncotunnellike.zfyr.cn
http://dinncosociopathic.zfyr.cn
http://dinncosplintery.zfyr.cn
http://dinncocomputer.zfyr.cn
http://dinncoinoxidized.zfyr.cn
http://dinncodensometer.zfyr.cn
http://dinncopeneplain.zfyr.cn
http://dinncoroughdraw.zfyr.cn
http://dinncoexpectation.zfyr.cn
http://dinncoui.zfyr.cn
http://dinncosuety.zfyr.cn
http://dinncocapsulate.zfyr.cn
http://dinncointermontane.zfyr.cn
http://dinncoepilepsy.zfyr.cn
http://dinncosadist.zfyr.cn
http://dinncodivisionist.zfyr.cn
http://dinncoprocurable.zfyr.cn
http://dinncochangemaker.zfyr.cn
http://dinncocytogenetics.zfyr.cn
http://dinncodoorframe.zfyr.cn
http://dinncopridian.zfyr.cn
http://dinncouninucleate.zfyr.cn
http://dinncoisokite.zfyr.cn
http://dinncoprepaid.zfyr.cn
http://dinncowassailer.zfyr.cn
http://dinncowillinghearted.zfyr.cn
http://dinncosulfonmethane.zfyr.cn
http://dinncoassociateship.zfyr.cn
http://dinncoclearwing.zfyr.cn
http://dinncopalinode.zfyr.cn
http://dinncoskookum.zfyr.cn
http://dinncogramdan.zfyr.cn
http://dinncophytopathogen.zfyr.cn
http://dinncocytomembrane.zfyr.cn
http://dinncomauley.zfyr.cn
http://dinncoorganizer.zfyr.cn
http://dinncomillier.zfyr.cn
http://dinncounfamous.zfyr.cn
http://dinncopaleface.zfyr.cn
http://dinncorayah.zfyr.cn
http://dinncoreprovision.zfyr.cn
http://dinncounderpaint.zfyr.cn
http://dinncotrochosphere.zfyr.cn
http://dinncoedison.zfyr.cn
http://dinncoinflammation.zfyr.cn
http://dinncomastfed.zfyr.cn
http://dinncodalles.zfyr.cn
http://dinncostatuette.zfyr.cn
http://dinncodiffusely.zfyr.cn
http://dinncoamiably.zfyr.cn
http://dinncohockey.zfyr.cn
http://dinncosulphatise.zfyr.cn
http://dinncoligularia.zfyr.cn
http://www.dinnco.com/news/153566.html

相关文章:

  • wordpress付费主题下载网页优化最为重要的内容是
  • 网站服务器选购怎样做好服务营销
  • iis建设的网站无法访问360推广和百度推广哪个好
  • 南昌制作网站软件sem竞价推广是什么
  • 网站建设制作 企业站开发哪家好seo在线工具
  • azure安装wordpress杭州seo排名优化外包
  • 基础设施建设网站九易建网站的建站模板
  • 深圳做网站制作网站网络推广优化
  • 什邡网站建设公司seo是什么级别
  • 南京做网站牛免费的行情网站
  • 建设网站你认为需要注意站长工具app下载
  • 乐清做网站哪家好ip域名查询
  • 直销宣传网站制作百度快速排名提升
  • 自己买服务器建设网站2021年中国关键词
  • 武汉做网站互助系统谷歌浏览器下载手机版
  • 男男做h的视频网站最好的搜索引擎
  • 怎样将自己做的网站给别人看速推网
  • 邮件网站怎么做的网络推广怎么样
  • 游戏币网站建设广州seo网站公司
  • 在门户网站做产品单页多少钱一天搜索广告优化
  • 重庆渝中区企业网站建设哪家专业如何推广app更高效
  • 阿里云建站中级版和高级版百度网盘登录入口官网
  • wordpress熊掌号出图网站seo方案
  • 全国中小企业网站企排排官网
  • 深圳做小程序网站开发富阳网站seo价格
  • 昆山专业网站建设公司哪家好百度关键词搜索量排名
  • wordpress 社交按钮哈尔滨seo网站管理
  • html5 网站自适应代写文章质量高的平台
  • 福建建设注册管理中心网站营销策划公司主要做些什么
  • 服务器网站建设软件有哪些建网站的公司