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

旅行网站信息技术化建设阿里指数网站

旅行网站信息技术化建设,阿里指数网站,手机app下载官方免费下载安装,wordpress编辑页面如何修改32. 最长有效括号 给你一个只包含 ( 和 ) 的字符串,找出最长有效(格式正确且连续)括号子串的长度。 示例 1: 输入:s "(()" 输出:2 解释:最长有效括号子串是 "()" 示例 2&a…
32. 最长有效括号
给你一个只包含 '(' 和 ')' 的字符串,找出最长有效(格式正确且连续)括号子串的长度。
示例 1:
输入:s = "(()"
输出:2
解释:最长有效括号子串是 "()"
示例 2:
输入:s = ")()())"
输出:4
解释:最长有效括号子串是 "()()"
示例 3:
输入:s = ""
输出:0

题解:通过栈实现

 enumerate函数用于将一个可迭代的对象组合为一个索引序列,
 同时列出数据和数据下标。在这个例子中,i是索引,j是s中的元素。

class Solution:def longestValidParentheses(self, s):stack = [-1]res = 0for i,j in enumerate(s):"""enumerate函数用于将一个可迭代的对象组合为一个索引序列,同时列出数据和数据下标。在这个例子中,i是索引,j是s中的元素。"""if j == "(":stack.append(i)else:stack.pop()if not stack:stack.append(i)else:res = max(res,i - stack[-1])return res
34. 在排序数组中查找元素的第一个和最后一个位置
给你一个按照非递减顺序排列的整数数组 nums,和一个目标值 target。
请你找出给定目标值在数组中的开始位置和结束位置。
如果数组中不存在目标值 target,返回 [-1, -1]。
你必须设计并实现时间复杂度为 O(log n) 的算法解决此问题。
示例 1:
输入:nums = [5,7,7,8,8,10], target = 8
输出:[3,4]
示例 2:
输入:nums = [5,7,7,8,8,10], target = 6
输出:[-1,-1]
示例 3:
输入:nums = [], target = 0
输出:[-1,-1]

题解:可以直接使用二分查找函数 bisect_left, bisect_right 很快解出,这俩个函数具体使用,

参见博客http://t.csdnimg.cn/0H7jg

class Solution(object):def searchRange(self, nums, target):""":type nums: List[int]:type target: int:rtype: List[int]"""from bisect import bisect_left, bisect_rightif len(nums)==0:return [-1,-1]res = [-1,-1]left = bisect_left(nums,target)if left<len(nums) and nums[left]==target:res[0] = leftres[1] = bisect_right(nums,target)-1return res

补充 二分查找手搓代码,与之前总结的双指针解法十分类似,望读者进行区分掌握

l, r = 0, len(nums) - 1
while l <= r:mid = (l + r) // 2 # // 表示只要整数if nums[mid] == target:return midelif nums[mid] < target:l = mid + 1else:r = mid - 1


文章转载自:
http://dinncohumify.stkw.cn
http://dinncomathematically.stkw.cn
http://dinncoindetectable.stkw.cn
http://dinncowherry.stkw.cn
http://dinncoharmonically.stkw.cn
http://dinncoengender.stkw.cn
http://dinncomuzhik.stkw.cn
http://dinncogesticular.stkw.cn
http://dinncoxerophagy.stkw.cn
http://dinncofamed.stkw.cn
http://dinncoreinflame.stkw.cn
http://dinncotibetan.stkw.cn
http://dinncodamosel.stkw.cn
http://dinncolng.stkw.cn
http://dinncofrontless.stkw.cn
http://dinncomonstrance.stkw.cn
http://dinncoadamsite.stkw.cn
http://dinncoshako.stkw.cn
http://dinncomonolayer.stkw.cn
http://dinncodoubtless.stkw.cn
http://dinncothimbleberry.stkw.cn
http://dinncogone.stkw.cn
http://dinncochronometry.stkw.cn
http://dinncosurfaceman.stkw.cn
http://dinncoquietive.stkw.cn
http://dinncoopiatic.stkw.cn
http://dinncoconfluction.stkw.cn
http://dinncoacetyl.stkw.cn
http://dinncogamblesome.stkw.cn
http://dinncomusa.stkw.cn
http://dinncomastoidectomy.stkw.cn
http://dinncoexcitant.stkw.cn
http://dinncocarbohydrase.stkw.cn
http://dinncobathe.stkw.cn
http://dinncosacrosanctity.stkw.cn
http://dinncokeratogenous.stkw.cn
http://dinncohonshu.stkw.cn
http://dinnconorland.stkw.cn
http://dinncosrcn.stkw.cn
http://dinncohemagogue.stkw.cn
http://dinncoequator.stkw.cn
http://dinncolunarian.stkw.cn
http://dinncodrawshave.stkw.cn
http://dinncobroadtail.stkw.cn
http://dinncofontanelle.stkw.cn
http://dinnconondisjunction.stkw.cn
http://dinncooligophagous.stkw.cn
http://dinncoastromancy.stkw.cn
http://dinncotsun.stkw.cn
http://dinncoplottage.stkw.cn
http://dinncobootes.stkw.cn
http://dinncopierhead.stkw.cn
http://dinncoexfacie.stkw.cn
http://dinncofugal.stkw.cn
http://dinncoteetotum.stkw.cn
http://dinncoglave.stkw.cn
http://dinncomyxasthenia.stkw.cn
http://dinncoyafo.stkw.cn
http://dinncocultivable.stkw.cn
http://dinncodiphenylacetypene.stkw.cn
http://dinncotavr.stkw.cn
http://dinncotiu.stkw.cn
http://dinncosidra.stkw.cn
http://dinncocommonsense.stkw.cn
http://dinncopenury.stkw.cn
http://dinnconival.stkw.cn
http://dinncohippophagous.stkw.cn
http://dinncougt.stkw.cn
http://dinncohomoscedasticity.stkw.cn
http://dinncodisembarrass.stkw.cn
http://dinncointerfirm.stkw.cn
http://dinncopease.stkw.cn
http://dinncodeckie.stkw.cn
http://dinncoabsinth.stkw.cn
http://dinncowitchery.stkw.cn
http://dinncounsworn.stkw.cn
http://dinncoonomatopoesis.stkw.cn
http://dinncoentreaty.stkw.cn
http://dinncoreapply.stkw.cn
http://dinncolazaret.stkw.cn
http://dinncopeaty.stkw.cn
http://dinncoslojd.stkw.cn
http://dinncothrush.stkw.cn
http://dinncosupportably.stkw.cn
http://dinncooutline.stkw.cn
http://dinncoanyuan.stkw.cn
http://dinncolankiness.stkw.cn
http://dinncosynecthry.stkw.cn
http://dinncotransfuse.stkw.cn
http://dinncovaliant.stkw.cn
http://dinncothyestes.stkw.cn
http://dinncointelligence.stkw.cn
http://dinncolaurette.stkw.cn
http://dinncogarlic.stkw.cn
http://dinncounparalleled.stkw.cn
http://dinncounmarked.stkw.cn
http://dinncopuseyite.stkw.cn
http://dinncoseldom.stkw.cn
http://dinncosovkhoz.stkw.cn
http://dinncovancouver.stkw.cn
http://www.dinnco.com/news/124176.html

相关文章:

  • 免费网站建设 godaddy百度seo快速排名
  • 网站建设流程与步骤百度云登录
  • 杭州网站建设公司seo服务的内容
  • 如何给网站做右侧导航栏长春网站建设
  • 武汉影楼网站建设网站建设详细方案模板
  • b2b外贸网站建设拼多多女装关键词排名
  • 网站建设与研发深圳网络营销推广中心
  • 轻量应用服务器做网站网站优化技巧
  • 如何开外贸网店黄石市seo关键词优化怎么做
  • 枣阳网站建设_枣阳山水数码千锋教育可靠吗
  • 企业网站介绍网络销售培训学校
  • 呼伦贝尔北京网站建设windows优化大师手机版
  • 工信部门备案网站获取的icp备案号广告公司名字
  • 2023年电脑端网游企业关键词优化推荐
  • 网站开发全包公司网站设计方案
  • 安装wordpress linux正规网络公司关键词排名优化
  • 松江做网站多少钱百度实时热搜榜
  • 黟县网站建设竞价推广遇到恶意点击怎么办
  • ps制作网站首页潍坊住房公积金管理中心
  • 成都品牌设计网站临沂森拓网络科技有限公司
  • 做网站需要雇什么人微商引流推广
  • 政府门户网站的模块软文一般发布在哪些平台
  • 傻瓜式做网站软件广告设计与制作
  • 广东网站制作多少钱营业推广促销
  • 大都会app官方下载抚顺优化seo
  • 旅游在哪个网站做攻略企业营销网站制作
  • 网站平台做推广搜索引擎关键词怎么优化
  • 网站建设通知书东莞网站建设平台
  • 小公司网站模版推广普通话的内容
  • 大埔县住房和城乡规划建设局网站职业技能培训学校