【Leetcode】020-Valid-Parentheses

2018-10-08  本文已影响0人  FLYNNNOTES

Qustion

Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.

An input string is valid if:

tips: Stack:first in last out

class Solution:
    def isValid(self, s):
        """
        :type s: str
        :rtype: bool
        """
        d = {')':'(', '}':'{', ']':'['}
        
        stack = []
        
        for i in s:
            if i in d and len(stack)!=0 and d[i]==stack[-1]:
                stack.pop()
            else:
                stack.append(i)
        
        return len(stack)==0
上一篇 下一篇

猜你喜欢

热点阅读