# 解题思路: 
# 创建一个字典映射关系 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

leetcode Valid Parentheses python的更多相关文章

  1. [LeetCode] Valid Parentheses 验证括号

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

  2. LeetCode: Valid Parentheses 解题报告

    Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', det ...

  3. [Leetcode] valid parentheses 有效括号对

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

  4. leetcode Longest Valid Parentheses python

    class Solution(object): def longestValidParentheses(self, s): """ :type s: str :rtype ...

  5. [leetcode]Valid Palindrome @ Python

    原题地址:https://oj.leetcode.com/problems/valid-palindrome/ 题意: Given a string, determine if it is a pal ...

  6. [leetcode]Valid Number @ Python

    原题地址:http://oj.leetcode.com/problems/valid-number/ 题意:判断输入的字符串是否是合法的数. 解题思路:这题只能用确定有穷状态自动机(DFA)来写会比较 ...

  7. LeetCode——Valid Parentheses

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

  8. [LeetCode] Valid Parentheses

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

  9. leetcode—Valid Parentheses

    1.问题描述 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if t ...

随机推荐

  1. js方法在对象中的状态

    在C#中,只有对象的字段存储在堆中,而方法则存储在一个方法表中.当实例化多个对象时,为字段分配了内存空间,而方法都指向一个方法表中的同一个方法. 如 而在JS中,字段和方法都属于值类型,都存储在堆中. ...

  2. DoNet开源项目-基于Amaze UI的点餐系统

    帮朋友做的点餐系统,主要是为了让顾客在餐桌上,使用微信扫描二维码,就可以直接点菜,吃完使用微信付款. 系统演示地址,账户名和密码均为:admin.(请不要删除admin用户) GitHub Clone ...

  3. UVA 10570 Meeting with Aliens

    题意: N个外星人围成一桌坐下,有序的排列指N在N-1与N+1中间,现在给出一个序列,问至少交换几次可以得到有序的序列. 分析: 复制一遍输入序列,放在原序列之后.相当于环.通过枚举,可以把最小交换次 ...

  4. Linux下进程的文件访问权限

    本文转自 http://blog.csdn.net/chosen0ne/article/details/10581883 对进程校验文件访问权限包括两个部分,一是确定进程的角色(属于哪个用户或者组), ...

  5. http://www.cnblogs.com/fczjuever/archive/2013/04/05/3000680.html

    http://www.cnblogs.com/fczjuever/archive/2013/04/05/3000680.html

  6. Android中的Menu

    Android中的设置按钮:长按或点击菜单键 1.长按选项: 布局文件: <LinearLayout xmlns:android="http://schemas.android.com ...

  7. bootstrap 3 のcheckbox-inline

         <div class="form-group">      <p class="control-label"><b> ...

  8. android上传json与服务器交互

    http://www.2cto.com/kf/201403/289328.html http://www.tuicool.com/articles/FZJR3eB

  9. php中__autoload()方法详解

    [导读] PHP在魔术函数__autoload()方法出现以前,如果你要在一个程序文件中实例化100个对象,那么你必须用include或者require包含进来100个类文件,或者你把这100个类定义 ...

  10. Flink Program Guide (7) -- 容错 Fault Tolerance(DataStream API编程指导 -- For Java)

    false false false false EN-US ZH-CN X-NONE /* Style Definitions */ table.MsoNormalTable {mso-style-n ...