【LeetCode每天一题】Valid Parentheses(有效的括弧)
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
An input string is valid if:
- Open brackets must be closed by the same type of brackets.
- Open brackets must be closed in the correct order.
Note that an empty string is also considered valid.
Example 1: Input: "()" Output: true
Example 2: Input: "()[]{}" Output: true
Example 3: Input: "(]" Output: false
Example 4: Input: "([)]" Output: false
Example 5: Input: "{[]}" Output: true
思路
我们可以设置一个辅助空间栈来保存括弧的左半边,然后进行遍历。当遍历到不是右半边括弧时,我们就弹出最上面的括弧来和当前括弧进行对比,查看是否是有效括弧。 时间复杂度为O(n)(n为字符串的长度), 空间复杂度为O(n)。
图示思路


一直到遍历完毕
代码
class Solution(object):
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
if len(s) < : # 空字符串直接返回
return True
tem_dict = {')':'(', '}':'{', ']':'['} # 设置辅助字典
stack = [] # 设置辅助栈
for i in s: # 进行遍历
if i not in tem_dict:
stack.append(i)
else:
if stack: # 右括弧进行选择
t = stack.pop()
if t != tem_dict[i]:
return False
else:
return False
if stack: # 栈不为空的话返回False
return False
return True
【LeetCode每天一题】Valid Parentheses(有效的括弧)的更多相关文章
- leetcode第20题--Valid Parentheses
Problem: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if ...
- LeetCode解题报告—— Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- LeetCode解题笔记 - 20. Valid Parentheses
这星期听别人说在做LeetCode,让他分享一题来看看.试了感觉挺有意思,可以培养自己的思路,还能方便的查看优秀的解决方案.准备自己也开始. 解决方案通常有多种多样,我觉得把自己的解决思路记录下来,阶 ...
- [Leetcode][Python]32: Longest Valid Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 32: Longest Valid Parentheseshttps://oj ...
- 【一天一道LeetCode】#32. Longest Valid Parentheses
一天一道LeetCode系列 (一)题目 Given a string containing just the characters '(' and ')', find the length of t ...
- LeetCode之“动态规划”:Valid Parentheses && Longest Valid Parentheses
1. Valid Parentheses 题目链接 题目要求: Given a string containing just the characters '(', ')', '{', '}', '[ ...
- 【LeetCode算法-20】Valid Parentheses
LeetCode第20题 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determin ...
- 【LeetCode】32. Longest Valid Parentheses (2 solutions)
Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...
- leetcode problem 32 -- Longest Valid Parentheses
Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...
- 【LeetCode练习题】Longest Valid Parentheses
Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...
随机推荐
- js将图片转换为base64
直接上代码: var img = "imgurl";//imgurl 就是你的图片路径 function getBase64Image(img) { var canvas = do ...
- jexus手动跨域设置
AP.NET MVC默认跨域方法如下: <system.webServer> <validation validateIntegratedModeConfiguration=&quo ...
- vue项目打包后一片空白及资源引入的路径报错解决办法
网上很多说自己的VUE项目通过Webpack打包生成的list文件,放到HBulider打包后,通过手机打开一片空白.这个主要原因是路径的问题. 1.记得改一下config下面的index.js中bu ...
- [No0000187]可能是把Java内存区域讲的最清楚的一篇文章
写在前面(常见面试题) 基本问题: 介绍下 Java 内存区域(运行时数据区) Java 对象的创建过程(五步,建议能默写出来并且要知道每一步虚拟机做了什么) 对象的访问定位的两种方式(句柄和直接指针 ...
- [No0000B2]ReSharper操作指南3/16-配置ReSharper与代码校错
配置ReSharper ReSharper功能具有默认配置,这些配置基于.NET世界中的约定和最佳实践.但是,每个功能都可以根据您的需求和喜好灵活调整. ReSharper首选项可以在以下位置进行配置 ...
- D. Who killed Cock Robin 湖北省大学程序设计竞赛
链接:https://www.nowcoder.com/acm/contest/104/C来源:牛客网 The Sparrow is for trial, at next bird assizes,w ...
- hive优化之并行执行任务
1.与Oracle并行技术一样,hive在执行mapreduce作业时也可以执行并行查询.针对于不同业务场景SQL语句的执行情况,有些场景下SQL的执行是需要分割成几段去执行的,而且期间并不全是存在依 ...
- Python:if __name__ == '__main__'
简介: __name__是当前模块名,当模块被直接运行时模块名为_main_,也就是当前的模块,当模块被导入时,模块名就不是__main__,即代码将不会执行. 关于代码if __name__ == ...
- Zabbix使用微信发送告警(附Python代码)
介绍 本文将介绍如何把zabbix告警接入到微信,通过微信企业号将告警信息发送到运维人员的微信上.本文适合于已经实现了邮件告警的小伙伴,因为需要在已经能实现告警的基础上进行,如果还不知道如何配置zab ...
- [administrative][CentOS] 新装系统时如何正确精准的选择基础环境和软件包
出于不同的目的,在进行全新CentOS安装的时候,我们到底应该如何作出选择. 是mininal,base server, base web server, 还是啥? 答案在这里: https://ac ...