# 解题思路: 
# 创建一个字典映射关系 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. Tcl 简单介绍及特性

    [简单介绍|特性] l  简单介绍 Tcl是一门产生于80年代末的语言,和Python一样,她是用c开发出来的.假设说C/Java/C++/C#为编译型语言的话,那么Python.Perl和Tcl就是 ...

  2. Silverlight学习(四) domainservice动态多条件查询

    上次讲了silverlight+MVVN+EF的简单框架,能够实现简单的数据CURD,但是多条件动态的查询一直没有实现.在网上查阅了很多资料,发现自己走了很多误区,代码很难调试正确. 这次的查询是基于 ...

  3. Hibernate学习之注解学习

    转自:http://blog.sina.com.cn/s/blog_935ebb670101dnre.html 1.类级别注解 @Entity   映射实体类 @Table    映射数句库表 @En ...

  4. HTML5之Viewport详解

    做移动Web开发也有一年多的时间了,虽然手机上浏览器对于PC上来说很友好了,但是手机各种设备的显示尺寸分辨率大小不一也要花大心思兼容它们. 关于HTML5中Viewport的文章Google,百度一搜 ...

  5. web2py官方文档翻译01

    第一章:介绍 介绍 web2py(web2py)是一个免费的开源web框架的敏捷开发安全的数据库驱动的web应用程序,这是用Python编写的Python(Python)和可编程.web2py是一个完 ...

  6. python排序(冒泡, 快速)

    之前用java时学习的一些基础算法,今天在python上也研究下. 1. 冒泡排序 算法步骤: 50   30   70  90 10 1)50 跟 30 比不用交换. 2)步数+1, 30 跟70比 ...

  7. JavaScript学习笔记(三)this关键字

    this是Javascript的关键字,代表在函数运行时,自动生成一个内部对象,只能在函数内部使用.例如: function test() { this.x = 1; } 随着函数的使用场合不同,th ...

  8. os模块实现遍历文件

    使用OS模块中的walk实现文件遍历. walk(top, topdown=True, onerror=None, followlinks=False) 从官方提供的doc中看到有四个参数 1> ...

  9. lua学习笔记(2)-常用调用

    assert(loadstring("math.max(7,8,9)"))dofile("scripts/xxx.lua")math.floor()math.r ...

  10. ListView的Item点击事件(消息传递)

    转载请保留原文出处“http://my.oschina.net/gluoyer/blog”,谢谢! 您可以到博客的“友情链接”中,“程序猿媛(最新下载)*.*”下载最新版本,持续更新!当前版本,也可直 ...