23/100. Valid Parentheses
给定一个字符串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相关推荐
- LeetCode Longest Valid Parentheses
原题链接在这里:https://leetcode.com/problems/longest-valid-parentheses/ 题目: Given a string containing just ...
- Longest Valid Parentheses leetcode java
题目: Given a string containing just the characters '(' and ')', find the length of the longest valid ...
- LeetCode 之 JavaScript 解答第20题 —— 有效的括号(Valid Parentheses)
Time:2019/4/11 Title: Valid Parentheses Difficulty: Easy Author: 小鹿 题目:Valid Parentheses Given a str ...
- LeetCode 32. Longest Valid Parentheses
问题链接 LeetCode 32. Longest Valid Parentheses 题目解析 给出只包含左右括号的字符串,返回最长的括号匹配字符串长度. 解题思路 括号匹配问题一般借助 栈,便于理 ...
- LeetCode算法入门- Longest Valid Parentheses -day12
LeetCode算法入门- Longest Valid Parentheses -day12 Given a string containing just the characters '(' and ...
- LeetCode算法入门- Valid Parentheses -day11
LeetCode算法入门- Valid Parentheses -day11 题目描述: Given a string containing just the characters '(', ')', ...
- 最长有效括号子串长度 c语言,LeetCode: Longest Valid Parentheses (求最长有效匹配括号子串的长度)...
题目描述: Given a string containing just the characters'(' and')', find the length of the longest valid ...
- Leetcode: Longest Valid Parentheses
Question Given a string containing just the characters '(' and ')', find the length of the longest v ...
- LeetCode --- Valid Parentheses
题目链接 Problem discription Given a string containing just the characters '(', ')', '{', '}', '[' and ' ...
最新文章
- c# tcp备忘及networkstream.length此流不支持查找解决
- hdu 4667 Building Fence 计算几何模板
- java设计模式 单例_java设计模式一(单例模式singleton)
- 【推荐系统】基于用户的协同过滤算法
- mysql数据库发布到web服务器上_web应用发布至服务器
- 一文详解物化视图改写
- 世界坐标系到观察坐标系的变换步骤_带你轻松认识不同坐标系下向量的“变脸”——基变换...
- cesium 圆形波纹
- 基于asp.net大学生就业管理系统#毕业设计
- 网站分析基础概念之初访者
- 使用UltraEdit编辑器之HelloWorld的实现
- php 协成wifi_2016最新协成wifi认证系统二次开发版源码 支持中文ssid 无加密无限制...
- Windows上查看MTU值和修改MTU的方法
- python程序设计基础之注释
- 将vscode打造为jetbrains主题
- 通过uart串口和printf函数打印
- MySQL中的字符集是啥?如何更改?
- SMPL:数据增强之处理pose和3d点
- yum linux centos安装mysql详细教程
- 数据分析师进阶必备6大数学利器
热门文章
- java string.join找不到_Java String join()用法及代码示例
- html5app微信登陆,基于h5+的微信登陆,hbuilder打包
- Mysql从库主键卡住_从库宕机引发的主键冲突
- c 指针地址 突然改变_开发笔记 - Cpp - 指针 - 1.1 如何理解指针
- Linux常用命令大全--有关磁盘空间的命令
- java编程double相乘_浅谈Java double 相乘的结果偏差小问题
- golang获取结构体中的tag_26. Go 语言中结构体的 Tag 用法
- 【Vulnhub靶机系列】DC3
- Python collection模块
- ajax的常见几种写法以及用法