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
leetcode Valid Parentheses python的更多相关文章
- [LeetCode] Valid Parentheses 验证括号
		
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
 - LeetCode: Valid Parentheses 解题报告
		
Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', det ...
 - [Leetcode] valid parentheses 有效括号对
		
Given a string containing just the characters'(',')','{','}','['and']', determine if the input strin ...
 - leetcode Longest Valid Parentheses python
		
class Solution(object): def longestValidParentheses(self, s): """ :type s: str :rtype ...
 - [leetcode]Valid Palindrome @ Python
		
原题地址:https://oj.leetcode.com/problems/valid-palindrome/ 题意: Given a string, determine if it is a pal ...
 - [leetcode]Valid Number @ Python
		
原题地址:http://oj.leetcode.com/problems/valid-number/ 题意:判断输入的字符串是否是合法的数. 解题思路:这题只能用确定有穷状态自动机(DFA)来写会比较 ...
 - LeetCode——Valid Parentheses
		
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
 - [LeetCode] Valid Parentheses
		
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
 - leetcode—Valid Parentheses
		
1.问题描述 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if t ...
 
随机推荐
- 九度OJ 题目1371:最小的K个数
			
题目描述: 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. 输入: 每个测试案例包括2行: 第一行为2个整数n,k(1< ...
 - MTK Android4.0.3 ICS 添加缅甸语Myanmar
			
最近几个项目需要添加缅甸语,借助网络资源,同时结合自身实践,成功添加缅甸语,现分享经验如下. 一. 前期工作: 准备Myanmar字库,下载地址:http://www.myordbok.com/mya ...
 - (原)工具篇-利用fis压缩项目
			
fis3 1.添加 fis-conf.js 到项目根目录中 fis-conf.js 内容如下 : //配置MD5版本控制 fis.match('*.{js,css,png,jpg}', { useHa ...
 - struts2 ActionSupport关联源码
			
 - poj 1450 Gridland
			
Gridland Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
 - 泛型编程中的Concept, Model和Policy
			
A crude explanation Concept A set of requirements on a type, e.g. a RandomAccessible concept require ...
 - 浏览器的重绘repaints与重排reflows深入分析
			
重绘是一个元素外观的改变所触发的浏览器行为,浏览器会根据元素的新属性重新绘制,使元素呈现新的外观,接下来将详细介绍,需要了解的朋友可以参考下: 在项目的交互或视觉评审中,前端同学常常会对一些交互效果质 ...
 - python 初学笔记 (一)
			
初学python第一天,希望自己真正了解计算机语言,并且做出成效. 写下学习笔记,记录学习进度,娱乐学习,不断成长. python详细介绍: python是什么?运用到哪里?有哪些在使用它? pyth ...
 - [Django] html 前端页面jQuery、图片等路径加载问题
			
严格的说这个话题应该属于一个html前端路径加载问题.为了实现一个局部更新页面的功能,简单了解了一下Ajax.Ajax是一个为了实现浏览器和服务器异步通信功能的模块.严格来说不是一个新的语言,只是JS ...
 - SPI Flash
			
使用了MX25L512的SPI接口的Flash 电路连接图: 总的大小512kb,即64kB,sector的大小为256 Bytes,block的大小为4k Bytes 调试时出现的问题: 1.Fla ...