20. Valid Parentheses
1.题目:
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.
这个题目不难理解,就是我们平时写程序时候,小括号,中括号,大括号都要对应的包起来,否则就有问题。
现在给定一个字符串,需要你来判断一下这个字符串是否满足该要求!
2.代码:
该问题,用栈的后进先出结构就很容易解决了。分三种情况,1. 下一个元素是左边的,直接入栈;2.下一个元素是右边的,要看能否和栈顶元素匹配,不匹配当然有问题了;3.匹配的话,刚好抵消掉栈顶元素,栈顶元素出栈;最后,判断下栈有没有完全抵消即可。
# -*-coding:utf-8-*-
class Solution(object):
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
left = ['(','[','{']
right = [')',']','}']
l = list(s)
stack = []
left_count,right_count = 0,0
#从左到右遍历字符串
while len(l)>0:
#如果这个字符是在left中,就写进stack
if l[0] in left:
stack.append(l[0])
#如果这个字符在right中,先判断stack,如果stack为空,直接返回False,如果stack的最后一位和该字符不匹配(否则会出现交叉情况,例如([)]),也直接返回False
#如果stack的最后一位和该字符刚好匹配,则从stack中弹出匹配到的字符
elif l[0] in right:
if len(stack)==0:return False
#print (l[0],' ',stack[-1])
#print (right.index(')'))
if right.index(l[0])==left.index(stack[-1]):
stack.pop()
else:
return False
#如果输入不是这些,也返回False,题目中说了 just the characters,只有这六个字符
else:
return False
l.remove(l[0])
#最后判断stack是否刚好抵消
return len(stack)==0 if __name__=='__main__':
s = '(('
a = Solution()
print (a.isValid(s))
20. Valid Parentheses的更多相关文章
- [Leetcode][Python]20: Valid Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 20: Valid Parentheseshttps://oj.leetcod ...
- 20. Valid Parentheses【leetcode】
20. Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
- leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、
20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...
- 《LeetBook》leetcode题解(20):Valid Parentheses[E]——栈解决括号匹配问题
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- LeetCode解题笔记 - 20. Valid Parentheses
这星期听别人说在做LeetCode,让他分享一题来看看.试了感觉挺有意思,可以培养自己的思路,还能方便的查看优秀的解决方案.准备自己也开始. 解决方案通常有多种多样,我觉得把自己的解决思路记录下来,阶 ...
- leetCode练题——20. Valid Parentheses
1.题目 20. Valid Parentheses——Easy Given a string containing just the characters '(', ')', '{', '}', ...
- 刷题20. Valid Parentheses
一.题目说明 这个题目是20. Valid Parentheses,简单来说就是括号匹配.在学数据结构的时候,用栈可以解决.题目难度是Medium. 二.我的解答 栈涉及的内容不多,push.pop. ...
- C# 写 LeetCode easy #20 Valid Parentheses
20.Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
- [LeetCode] 20. Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [leetcode]20. Valid Parentheses有效括号序列
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
随机推荐
- linux系统硬件配置查看方法
一:查看cpu more /proc/cpuinfo | grep "model name" grep "model name" /proc/cpuinfo 如 ...
- oracle--逻辑对象--bai
1 序列 sequence oracle特有.实现"自增"或"自减"的逻辑对象. 2 同义词 synonym 对表取别名,该别名被永久存储. 比视图更省资源. ...
- 四、基于hadoop的nginx访问日志分析---top 10 request
代码: # cat top_10_request.py #!/usr/bin/env python # coding=utf-8 from mrjob.job import MRJob from mr ...
- xpath 学习一: 节点
xpath 中,有七种类型的节点: 元素.属性.文本.命名空间.处理指令.注释.以及根节点 树的根成为文档节点或者根节点. 节点关系: Parent, Children, sibling(同胞), A ...
- Django基础,Day6 - 单元测试tests
在django项目app目录下,有个tests.py,我们通常可以直接在这文件中写我们的单元测试代码. test for a model 根据前面章节的操作步骤下来,在Question Model中有 ...
- Yii2 定时任务创建(Console 任务)
Yii2的定时任务可以有两种写法,原理都是通过服务器的定时任务去调用 1.通过调用指定的URL访问 就相当于在浏览器中访问 2.通过console调用 下面我们就来说说Console 是如何实现定时任 ...
- 美团HD(2)-设置导航栏内容
DJHomeViewController.m #import "DJHomeViewController.h" #import "DJConstantValue.h&qu ...
- Xcode查找内存泄漏
- nodejs net模块
net模块是同样是nodejs的核心模块.在http模块概览里提到,http.Server继承了net.Server,此外,http客户端与http服务端的通信均依赖于socket(net.Socke ...
- TCP学习之五:客户端、服务端异步传输字符串
参考学习张子阳大神的博客:http://www.cnblogs.com/JimmyZhang/category/101698.html 消息发送接口: 消息接收接口: 客户端: 服务端: 消息发送类: ...