### 20. Valid Parentheses

题目:

<https://leetcode.com/problems/valid-parentheses/>


难度:

Easy


### 思路：

因为一共只有三种状况"(" -> ")", "[" -> "]", "{" -> "}".

一遇到左括号就入栈，右括号出栈，这样来寻找对应

需要检查几件事：

- 出现右括号时stack里还有没有东西
- 出stack时是否对应
- 最终stack是否为空



```python
class Solution(object):
    def isValid(self, s):
        """
        :type s: str
        :rtype: bool
        """
        leftP = '([{'
        rightP = ')]}'
        stack = []
        for char in s:
            if char in leftP:
                stack.append(char)
            if char in rightP:
                if not stack:
                    return False
                tmp = stack.pop()
                if char == ')' and tmp != '(':
                    return False
                if char == ']' and tmp != '[':
                    return False       
                if char == '}' and tmp != '{':
                    return False
        return stack == []
```

