题目:给定一个只包括’(‘ , ‘)’ , ‘{‘ , ‘}’ , ‘[‘ , ‘]’的字符串,判断字符串是否有效
    例:

    1. 输入: "()"
    2. 输出: true
    输入: "()[]{}"
    输出: true
    
    输入: "(]"
    输出: false
    
    输入: "([)]"
    输出: false
    
    输入: "{[]}"
    输出: true
    

    题解:
    一、栈

    class Solution:
        def isValid(self, s: str) -> bool:
            dic = {'{': '}',  '[': ']', '(': ')', '?': '?'}
            stack = ['?']
            for c in s:
                if c in dic: stack.append(c)
                elif dic[stack.pop()] != c: return False 
            return len(stack) == 1
    
    作者:jyd
    链接:https://leetcode-cn.com/problems/valid-parentheses/solution/valid-parentheses-fu-zhu-zhan-fa-by-jin407891080/
    来源:力扣(LeetCode)
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
    

    知识点:
    栈(stack)
    python内置list.pop()方法等价于出栈,list.append()等价于入栈。

    stack=[1,2,3,4,5]
    
    stack.pop()=5
    
    stack.append(6)
    stack=[1, 2, 3, 4, 6]
    

    字典(dict)

    dic={'name':'田所浩二','age':24,'address':'野兽邸'}
    
    dic.get('name')='田所浩二'
    
    dic.keys()=dict_keys(['name', 'age', 'address'])
    

    dict_keys是一个unsubscriptable但是iterable的类型,所以使用iterator访问。

    for k in dic.keys():
        print(k)
    
    输出:
    name
    age
    address