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

做网站需要哪些东西/东莞网站建设推广品众

做网站需要哪些东西,东莞网站建设推广品众,wordpress网站换域名,安丘市建设局官方网站未经博主同意,禁止瞎JB转载。 LeetCode20有效的括号 https://leetcode-cn.com/problems/valid-parentheses/description/ 我的解法: 示例如下: 利用了栈的思想,使用两个列表实现配对,与官方提供的解答是一样的&#xf…

未经博主同意,禁止瞎JB转载。

LeetCode20有效的括号

https://leetcode-cn.com/problems/valid-parentheses/description/

我的解法:

示例如下:

利用了栈的思想,使用两个列表实现配对,与官方提供的解答是一样的,但是写的就比较麻烦。

 1 class Solution(object):
 2     def isValid(self, s):
 3         """
 4         :type s: str
 5         :rtype: bool
 6         """
 7         # "(([]){})"
 8         s = list(s)
 9         n = len(s)
10         if n == 0:
11             return True
12         if n % 2 == 1:
13             return False
14         ret = []
15         i = 0
16         while len(s):
17             if s[0] == '(' or s[0] == '[' or s[0] == '{':
18                 ret.append(s.pop(0))
19                 if len(ret)>len(s):
20                     return False
21             else:
22                 if len(ret) == 0:
23                     return False
24                 while len(ret):
25                     if ret[-1] == '(' and s[0] == ')' or ret[-1] == '[' and s[0] == ']' or ret[-1] == '{' and s[0] == '}':
26                         ret.pop()
27                         s.pop(0)
28                     elif s[0] == '(' or s[0] == '[' or s[0] == '{':
29                         break
30                     else:
31                         return False
32         if len(ret):
33             return False
34         return True

 官方的解答:

https://leetcode-cn.com/problems/valid-parentheses/solution/

 1 class Solution(object):
 2     def isValid(self, s):
 3         """
 4         :type s: str
 5         :rtype: bool
 6         """
 7 
 8         # The stack to keep track of opening brackets.
 9         stack = []
10 
11         # Hash map for keeping track of mappings. This keeps the code very clean.
12         # Also makes adding more types of parenthesis easier
13         mapping = {")": "(", "}": "{", "]": "["}
14 
15         # For every bracket in the expression.
16         for char in s:
17 
18             # If the character is an closing bracket
19             if char in mapping:
20 
21                 # Pop the topmost element from the stack, if it is non empty
22                 # Otherwise assign a dummy value of '#' to the top_element variable
23                 top_element = stack.pop() if stack else '#'
24 
25                 # The mapping for the opening bracket in our hash and the top
26                 # element of the stack don't match, return False
27                 if mapping[char] != top_element:
28                     return False
29             else:
30                 # We have an opening bracket, simply push it onto the stack.
31                 stack.append(char)
32 
33         # In the end, if the stack is empty, then we have a valid expression.
34         # The stack won't be empty for cases like ((()
35         return not stack

 

转载于:https://www.cnblogs.com/kianqunki/p/9759075.html

http://www.lbrq.cn/news/1357345.html

相关文章:

  • 下载手机软件的app/武汉seo技术
  • 展示型网站建设价格/怎么推广一个网站
  • 网站怎么做cp备案号/一个完整的产品运营方案
  • 做传感器交易的网站/如何在网站上推广自己的产品
  • 建筑工地网站有哪些/购买链接怎么买
  • 不同的网站 做301/网页设计排版布局技巧
  • 建设商务公司网站/大作设计网站
  • 揭阳 网站建设/视频号怎么推广流量
  • 怎么建网站手机版/北京seo顾问外包
  • 廊坊市做网站/什么是营销渠道
  • 做网站数据库怎么整/西安网站建设维护
  • 网站开发个人简介范文/域名解析ip
  • 网站开发服务承诺书/推广员是做什么的
  • 武汉新闻网/网站首页的优化
  • asp网站开发人员招聘/如何申请一个网站域名
  • 大访问量的网站怎么做优化/网站接广告平台
  • 网站建设基本流程心得/网络营销的基本流程
  • 深圳品牌网站设计推广/网站设计与网页制作
  • 平台网站怎么做的好/b站怎么推广
  • phpnow wordpress/网络推广优化
  • 史上最强大的搜索神器/seo整站优化多少钱
  • 做相册网站/网络营销的手段有哪些
  • 目前最好的免费网站/哈尔滨seo推广优化
  • 聊城冠县网站建设/三叶草gw9356
  • 化州网站建设/网络营销特点
  • 深圳网站建设 贴吧/百度收录快速提交
  • 海口模板建站平台/真实有效的优化排名
  • 重庆市建设工程造价站/网络推广怎么找客户资源
  • 网站建设公司yu/电商网站卷烟订货流程
  • 辽宁做网站和优化哪家好/免费线上培训平台
  • Mysql常用语句
  • GPT-5的诞生之痛:AI帝国的现实危机
  • 【C++】Stack and Queue and Functor
  • JavaScript 中的流程控制语句详解
  • 第13章 文件输入/输出
  • 2025年6月电子学会青少年软件编程(C语言)等级考试试卷(四级)