给定一个字符串s,它只包括“(”、“)”、“[”、“]”、“{”、“}”,判断该字符串是否合法。

使用栈的数据结构(先进后出),其实就是Python里的list列表。
①若长度为奇数个,该字符串不合法,返回False。
②若长度为偶数个:
若为右边的括号,且正好和前面一个字符匹配,则删除前一个字符;
若为左边的括号,则添加到列表的末尾;
否则,则返回False。
③最后,若L为空,则返回True。

class Solution:def isValid(self, s):""":type s: str:rtype: bool"""if not s:return Truelength = len(s)if length%2 != 0: #奇数个return FalseL = [s[0]]i = 1while i<length:if s[i]==')' and L[-1]=='(':L.pop()elif s[i]==']' and L[-1]=='[':L.pop()elif s[i]=='}' and L[-1]=='{':L.pop()elif s[i]=='(' or s[i]=='[' or s[i]=='{':L.append(s[i])else:return Falsei += 1if not L:return Trueelse:return False

其它思路:
使用字典dict。

paren = {'[':']', '(':')', '{':'}'}
valid = []for i in s:if i not in paren:if not valid or i!=paren[valid[-1]]:return Falsevalid.pop()else:valid.append(i)
return valid == []

23/100. Valid Parentheses相关推荐

  1. LeetCode Longest Valid Parentheses

    原题链接在这里:https://leetcode.com/problems/longest-valid-parentheses/ 题目: Given a string containing just ...

  2. Longest Valid Parentheses leetcode java

    题目: Given a string containing just the characters '(' and ')', find the length of the longest valid ...

  3. LeetCode 之 JavaScript 解答第20题 —— 有效的括号(Valid Parentheses)

    Time:2019/4/11 Title: Valid Parentheses Difficulty: Easy Author: 小鹿 题目:Valid Parentheses Given a str ...

  4. LeetCode 32. Longest Valid Parentheses

    问题链接 LeetCode 32. Longest Valid Parentheses 题目解析 给出只包含左右括号的字符串,返回最长的括号匹配字符串长度. 解题思路 括号匹配问题一般借助 栈,便于理 ...

  5. LeetCode算法入门- Longest Valid Parentheses -day12

    LeetCode算法入门- Longest Valid Parentheses -day12 Given a string containing just the characters '(' and ...

  6. LeetCode算法入门- Valid Parentheses -day11

    LeetCode算法入门- Valid Parentheses -day11 题目描述: Given a string containing just the characters '(', ')', ...

  7. 最长有效括号子串长度 c语言,LeetCode: Longest Valid Parentheses (求最长有效匹配括号子串的长度)...

    题目描述: Given a string containing just the characters'(' and')', find the length of the longest valid ...

  8. Leetcode: Longest Valid Parentheses

    Question Given a string containing just the characters '(' and ')', find the length of the longest v ...

  9. LeetCode --- Valid Parentheses

    题目链接 Problem discription Given a string containing just the characters '(', ')', '{', '}', '[' and ' ...

最新文章

  1. Tomcat的配置及优化
  2. Linux下动态共享库加载时的搜索路径详解
  3. ue4 设置intellisence_UE4.22编辑器界面操控设置(4)
  4. iOS开发UI篇—懒载入
  5. ReactNative开发工具有这一篇足矣
  6. Windows Server 2008 将与 Visual Studio 2008 和 SQL Server 2008 于2008年2月27 日在洛杉矶共同发布...
  7. 【转载】深入分析 ThreadLocal 内存泄漏问题
  8. win10系统文件拖拽卡顿_IT技巧分享16:如何让win10摆脱卡顿
  9. m7405d粉盒清零方法_联想m7605d清零方法
  10. python求均值函数_python求列表平均值函数
  11. c++字符串中元音字母转置
  12. 中小企业OA系统自动办公软件
  13. Windows下安装igraph
  14. 快手之家(aardio.net) - 感谢信
  15. 【项目管理软件盛宴】谁才是人气王?
  16. 东南西北十字图_怎么辨别东南西北的方向 分清东南西北口诀与窍门技巧(图)...
  17. 十个免费的Web负载/压力测试工具
  18. “熊猫烧香”病毒简介及特征
  19. 【系统之家首发】Ghost_Windows7_sp1_Ultimate_x86V2011.10.10 【OEM 通用版】Windows7旗舰版好人一个出品
  20. python小波变换1-理论

热门文章

  1. java test报错_Java 单元测试报错
  2. ora-01740: 标识符中缺失双引号_sql语句中单引号的使用
  3. python to sql_python的to_sql那点儿事
  4. gps天线拆解图片_华为 畅享 Z 拆解:揭秘千元5G手机物料成本是多少
  5. linux 编译报错 not a directory,Linux常见英文报错中文翻译
  6. 嵌入式 linux 启动脚本 编写,[9构建嵌入式linux系统项目-启动脚本的编写.ppt
  7. 计算机usb共享网络泄密,杜绝USB泄密 MyUSBOnly
  8. java python rsa加密_实现Java加密,Python解密的RSA非对称加密算法功能
  9. 一段树状无限制级代码
  10. log4j debug写法