zoukankan      html  css  js  c++  java
  • leetcode Valid Parentheses python

    # 解题思路: 
    # 创建一个字典映射关系 dicts
    # 使用一个栈stk 遍历字符串s 得到一个新的字符串curItem 如果lastItem在dicts中的value和它相等 不做任何操作
    # 如果不等 入栈 有lastItem的 先append lastItem 然后是curItem
    #
    # 最后判断如果stk为空说明所给字符串匹配 return true


    class
    Solution(object): def isValid(self, s): """ :type s: str :rtype: bool """ strLen = len(s) if strLen <= 1: return False dicts={"(":")","{":"}","[":"]"} stk=list() for i in xrange(strLen): lastItem=None if len(stk) > 0: lastItem = stk.pop() curItem = s[i] if len(stk) == 0 and lastItem == None: stk.append(curItem) elif dicts.has_key(lastItem) and curItem == dicts[lastItem]: pass else: if lastItem: stk.append(lastItem) stk.append(curItem) if len(stk) == 0: return True else: return False
  • 相关阅读:
    [Postman]历史(8)
    [Postman]响应(7)
    [Postman]请求(6)
    [Postman]查找替换(5)
    ORA-02050故障诊断一例
    转 js实践篇:例外处理Try{}catch(e){}
    转 PHP
    HTML DOM getElementById() 方法
    地点选择
    9i 和 11 g 区别
  • 原文地址:https://www.cnblogs.com/allenhaozi/p/4985114.html
Copyright © 2011-2022 走看看