[Leetcode][Python]20: Valid Parentheses

# -*- coding: utf8 -*-‘‘‘__author__ = ‘[email protected]‘

20: Valid Parentheseshttps://oj.leetcode.com/problems/valid-parentheses/

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.

===Comments by Dabay===用一个stack来保存左括号,如果来的是对应的右括号,就pop一个;如果来的是不对应的右括号,直接return False。‘‘‘

class Solution:    # @return a boolean    def isValid(self, s):        stack = []        for c in s:            if c in "([{":                stack.append(c)            else:                if len(stack) == 0:                    return False                if (                    (c == ")" and stack[-1] == "(") or                    (c == "]" and stack[-1] == "[") or                    (c == "}" and stack[-1] == "{")                ):                    stack.pop()                else:                    return False        else:            return len(stack) == 0

def main():    sol = Solution()    print sol.isValid("()[]{}")

if __name__ == ‘__main__‘:    import time    start = time.clock()    main()    print "%s sec" % (time.clock() - start)
时间: 2024-10-14 02:42:27

[Leetcode][Python]20: Valid Parentheses的相关文章

LeetCode:20. Valid Parentheses(Easy)

1. 原题链接 https://leetcode.com/problems/valid-parentheses/description/ 2. 题目要求 给定一个字符串s,s只包含'(', ')', '{', '}', '[' 和 ']'. 合法:形如"()[]"."{[()]}" 不合法:形如"([)]"."[[((" 判断所给字符串s是否合法. 3. 解题思路 对字符串s转换成字符数字进行遍历. 利用栈,遇到左半边字符:'

【LeetCode】20. Valid Parentheses

题目: 思路:用Stack基本操作完成. 要检测输入字符是否满足这个条件,一个非常合适的数据结构是stack,后进先出的特征正好满足检测的需求.在检测的时候,每次检查一个字符,如果是左括号,就入栈,如果是右括号,并且右括号和当前栈顶符号是左右配对的,那么就弹出栈顶并且进行下一次检测,如果不满足上面两种情况,就说明检查到了一个非法字符,返回false. public class Solution { public boolean isValid(String s) { if(s.length()=

【leetcode 】20. Valid Parentheses

这题需要注意: 栈的空指针 全局变量stack需要clear 输入结束时栈长度不为空 public class Solution { private static final Stack<Character> stack = new Stack<Character>(); public boolean isValid(String s) { stack.clear(); for (int i = 0; i < s.length(); ++i) { char c = s.cha

LeetCode解题笔记 - 20. Valid Parentheses

这星期听别人说在做LeetCode,让他分享一题来看看.试了感觉挺有意思,可以培养自己的思路,还能方便的查看优秀的解决方案.准备自己也开始. 解决方案通常有多种多样,我觉得把自己的解决思路记录下来,阶段性查看,一定能对自己有帮助. 这是我做的第一题,所以记录也从这题开始,之后尽力以简短的说明,描述出思路,方便以后能回顾到简介明了的记录. 20. Valid Parentheses Given a string containing just the characters '(', ')', '{

[LeetCode]20 Valid Parentheses 有效的括号

[LeetCode]20 Valid Parentheses 有效的括号 Description 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 brac

20. Valid Parentheses【leetcode】

20. Valid Parentheses 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 "(]&quo

leetCode 20. Valid Parentheses 字符串

20. Valid Parentheses 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 "(]&quo

leetcode题解:Valid Parentheses(栈的应用-括号匹配)

题目: 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 "([)]&

[LeetCode] 032. Longest Valid Parentheses (Hard) (C++)

指数:[LeetCode] Leetcode 指标解释 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 032. Longest Valid Parentheses (Hard) 链接: 题目:https://oj.leetcode.com/problems/longest-valid-parentheses/ 代码(github):https://github.com/illuz/leetcode 题意: 问一个字