20. Valid Parentheses

来源:互联网 发布:千里走单骑知乎 编辑:程序博客网 时间:2024/05/20 01:09

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.

概述:括号匹配,出门右转与C码解题方法

class Solution(object):    def isValid(self, s):      left = ['(','[','{']      right = [')',']','}']      match = {')':'(',']':'[','}':'{'}      result = []      count = len(s)      for i in xrange(count):        if s[i] in left:          result.append(s[i])        if s[i] in right:          if len(result)!=0 and match[s[i]]==result[-1]:            result.pop()          else:            return False      if len(result) == 0:        return True      else:        return False


原创粉丝点击