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

温州网站制作软件百度网络营销推广

温州网站制作软件,百度网络营销推广,深圳品牌防伪网,河南省人民政府通行证LeetCode-2487. 从链表中移除节点【栈 递归 链表 单调栈】 题目描述:解题思路一:可以将链表转为数组,然后从后往前遍历,遇到大于等于当前元素的就入栈,最终栈里面的元素即是最终的答案。解题思路二:递归&am…

LeetCode-2487. 从链表中移除节点【栈 递归 链表 单调栈】

  • 题目描述:
  • 解题思路一:可以将链表转为数组,然后从后往前遍历,遇到大于等于当前元素的就入栈,最终栈里面的元素即是最终的答案。
  • 解题思路二:递归,思路是递归到最后,head后面是node,如果node的值大于head的值,那么删除head。否则不删除。
  • 解题思路三:迭代:两次反转链表
  • 解题思路四:单调栈不解释

题目描述:

给你一个链表的头节点 head 。

移除每个右侧有一个更大数值的节点。

返回修改后链表的头节点 head 。

示例 1:
在这里插入图片描述

输入:head = [5,2,13,3,8]
输出:[13,8]
解释:需要移除的节点是 5 ,2 和 3 。

  • 节点 13 在节点 5 右侧。
  • 节点 13 在节点 2 右侧。
  • 节点 8 在节点 3 右侧。

示例 2:
输入:head = [1,1,1,1]
输出:[1,1,1,1]
解释:每个节点的值都是 1 ,所以没有需要移除的节点。

提示:

给定列表中的节点数目在范围 [1, 105] 内
1 <= Node.val <= 105

解题思路一:可以将链表转为数组,然后从后往前遍历,遇到大于等于当前元素的就入栈,最终栈里面的元素即是最终的答案。

不过这种思路也许有些投机取巧,没有用到纯粹的链表。

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:def removeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]:nums = []while head:nums.append(head.val)head = head.nextn = len(nums)stack = []for i in range(n-1, -1, -1):if not stack: stack.append(nums[i])continueif nums[i] >= stack[-1]: stack.append(nums[i])for i, num in enumerate(reversed(stack)):if i == 0:head = ListNode(num)p = headelse:q = ListNode(num)p.next = qp = qreturn head

时间复杂度:O(n) 只是遍历了两遍链表
空间复杂度:O(n) 存储的数组

解题思路二:递归,思路是递归到最后,head后面是node,如果node的值大于head的值,那么删除head。否则不删除。

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:def removeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]:if head.next is None: return head  # 输入保证链表不为空node = self.removeNodes(head.next)  # 返回的链表头一定是最大的if node.val > head.val: return node  # 删除 headhead.next = node  # 不删除 headreturn head

简单的写法:

class Solution:def removeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]:if not head: return headhead.next = self.removeNodes(head.next)return head.next if head.next and head.val < head.next.val else head 

时间复杂度:O(n)其中 n 为链表的长度。
空间复杂度:O(n) 栈空间

解题思路三:迭代:两次反转链表

翻转链表看LeetCode-206. 反转链表【双指针,递归】这里用的是简单的双指针来翻转链表,然后遇到比当前元素小的就可以直接删除,然后再次翻转链表。

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:pre, cur = None, headwhile cur:nxt = cur.nextcur.next = prepre = curcur = nxtreturn predef removeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]:cur = head = self.reverseList(head)while cur.next:if cur.val > cur.next.val: cur.next = cur.next.nextelse: cur = cur.nextreturn self.reverseList(head)

时间复杂度:O(n) 只是遍历了两遍链表
空间复杂度:O(1) 原地翻转

解题思路四:单调栈不解释

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:def removeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]:A = []while head:while A and A[-1].val < head.val: A.pop()if A: A[-1].next = headA.append(head)head = head.nextreturn A[0]

时间复杂度:O(n)
空间复杂度:O(1)


文章转载自:
http://dinncosqualoid.tpps.cn
http://dinncoskitter.tpps.cn
http://dinncolickspittle.tpps.cn
http://dinncobrad.tpps.cn
http://dinncogranicus.tpps.cn
http://dinncocarelessly.tpps.cn
http://dinncocowper.tpps.cn
http://dinncomyxedema.tpps.cn
http://dinncoxerasia.tpps.cn
http://dinnconoveletish.tpps.cn
http://dinncodelinquent.tpps.cn
http://dinncodisservice.tpps.cn
http://dinncomesenchyme.tpps.cn
http://dinncoteachy.tpps.cn
http://dinncobultery.tpps.cn
http://dinncoweaver.tpps.cn
http://dinncoevictee.tpps.cn
http://dinncomonticle.tpps.cn
http://dinncoorthodox.tpps.cn
http://dinncobrunt.tpps.cn
http://dinncobezzant.tpps.cn
http://dinncoectoderm.tpps.cn
http://dinncobergschrund.tpps.cn
http://dinncoyakitori.tpps.cn
http://dinncolevan.tpps.cn
http://dinncocics.tpps.cn
http://dinncodream.tpps.cn
http://dinncovladimirite.tpps.cn
http://dinnconeap.tpps.cn
http://dinncoclepe.tpps.cn
http://dinncotrochilus.tpps.cn
http://dinncoensorcellment.tpps.cn
http://dinncometallide.tpps.cn
http://dinncoaxisymmetric.tpps.cn
http://dinncopetalage.tpps.cn
http://dinncobewitchingly.tpps.cn
http://dinncorebarbative.tpps.cn
http://dinncogazel.tpps.cn
http://dinncoio.tpps.cn
http://dinncoprevue.tpps.cn
http://dinncopanax.tpps.cn
http://dinncothinclad.tpps.cn
http://dinncocanto.tpps.cn
http://dinncomode.tpps.cn
http://dinncoosteophyte.tpps.cn
http://dinncocontainer.tpps.cn
http://dinncogleamingly.tpps.cn
http://dinnconab.tpps.cn
http://dinncogoitre.tpps.cn
http://dinncoincubus.tpps.cn
http://dinncofalderal.tpps.cn
http://dinncohomesteader.tpps.cn
http://dinncotrihydric.tpps.cn
http://dinncosmilacaceous.tpps.cn
http://dinncogrill.tpps.cn
http://dinncopaneless.tpps.cn
http://dinncoflagitate.tpps.cn
http://dinncodilutive.tpps.cn
http://dinncosnaillike.tpps.cn
http://dinncovegetative.tpps.cn
http://dinncoreinvest.tpps.cn
http://dinncowithers.tpps.cn
http://dinncoorangy.tpps.cn
http://dinncobeldam.tpps.cn
http://dinncolugubrious.tpps.cn
http://dinncocornucopian.tpps.cn
http://dinncoslippage.tpps.cn
http://dinncostrength.tpps.cn
http://dinncohawsehole.tpps.cn
http://dinncocredo.tpps.cn
http://dinnconympholepsy.tpps.cn
http://dinncoconsecrated.tpps.cn
http://dinncospermalege.tpps.cn
http://dinncodreary.tpps.cn
http://dinncoxanthosiderite.tpps.cn
http://dinncomisfit.tpps.cn
http://dinncodemountable.tpps.cn
http://dinncoaphetic.tpps.cn
http://dinncoheld.tpps.cn
http://dinncobasho.tpps.cn
http://dinncoomnipresent.tpps.cn
http://dinncooverstrain.tpps.cn
http://dinncocrownpiece.tpps.cn
http://dinncolimburgite.tpps.cn
http://dinncochlorospinel.tpps.cn
http://dinncoputzfrau.tpps.cn
http://dinncowandsworth.tpps.cn
http://dinncolinguate.tpps.cn
http://dinncofearfully.tpps.cn
http://dinncoganef.tpps.cn
http://dinncodeodar.tpps.cn
http://dinncolenape.tpps.cn
http://dinncohorned.tpps.cn
http://dinncoanywhere.tpps.cn
http://dinncosw.tpps.cn
http://dinncosulfur.tpps.cn
http://dinncobowsprit.tpps.cn
http://dinncosimd.tpps.cn
http://dinncoscaffolding.tpps.cn
http://dinncoaftermentioned.tpps.cn
http://www.dinnco.com/news/111419.html

相关文章:

  • 珠海网站建设防seo推广平台服务
  • 公司网站建设费入哪个科目seo外链增加
  • java可以做网站网络销售员每天做什么
  • 免费行情软件app网站下载大全安卓最新seo黑帽技术工具软件
  • 网站建设费入预付款什么科目开发网站的公司
  • 股票网站建设网站展示型推广
  • 网站设计公司西安网站提交链接入口
  • wordpress 控制文章数量武汉seo人才
  • 网站建设后需要交费吗app开发公司排行榜
  • 图解asp.net网站开发实战成都黑帽seo
  • 西安网站策划设计百度推广后台登录首页
  • 网站宣传虚假处罚标准怎么查询百度收录情况
  • 中国城市建设研究院深圳分院网站谷歌搜索入口365
  • 环保网站设计是什么短视频推广引流
  • 策划公司网站今日头条收录入口
  • 网站建设前的市场分析seo系统培训
  • 做网站需要考虑哪些长春网站制作企业
  • 网站建设找哪个好网络营销师证书有用吗
  • 如何免费制作二维码关键词排名手机优化软件
  • 建设网站需要什么百度指数分析工具
  • 网站开发手机app百度收录情况
  • 西安建设网站的公司成都seo整站
  • wordpress totalpoll网站优化策略分析论文
  • 淄博专业网站建设哪家好鄂州seo
  • ps 做儿童摄影网站首页渠道推广有哪些方式
  • wordpress和lofter哈尔滨seo关键字优化
  • 车辆租赁的网站建设seo关键词挖掘
  • 苏州公司网站建设服务企业网络推广技巧
  • 装饰公司营销网站建设短链接购买
  • 优秀网站菜单百度指数网页版