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 ...
随机推荐
- IIS中如何建立FTP服务
文件传输协议 (FTP) 是一个标准协议,可用来通过 Internet 将文件从一台计算机移到另一台计算机.这些文件存储在运行 FTP 服务器软件的服务器计算机上.然后,远程计算机可以使用 FTP 建 ...
- 牛掰的图片等比缩放js代码
function resizeImg(img,oAW,oAH){ var oimgW = img.width, oimgH = img.height, oimg = img, oY = (oimgH/ ...
- 卸载cloudera manager
卸载Cloudera-Manager sudo /usr/share/cmf/uninstall-cloudera-manager.sh 一直选择Yes 就好 卸载完成后,它会问你是否要将databa ...
- gcc 编译和链接
1.现在对两个文件生成可执行文件 //thanks.c #include <stdio.h> int main(void) { printf("Hello World\n&quo ...
- 运维人员:走好你的IT运维路
转自 http://os.51cto.com/art/201303/387120.htm 现阶段,大多数运维人员只是处于被动低效率手工救火的状态,企业对其重视程度不高,导致部分运维人员对自己的 ...
- PHP简易计算器方法1
<?phpheader("content-type:text/html;charset=utf-8");session_start();?><!DOCTYPE h ...
- [Backbone.js]如何处理Model里面嵌入的Collection?
写了近半个月的backbone.js代码,从一开始的todo到现在做仿微信的网页聊天,其中最大的困惑就在于如何处理比较复杂的Model,其内嵌了一个或者多个Collections. 假设我们有一个Pe ...
- centos6.5图形界面NetworkManager 配置ip文件位置
请教一个关于网络配置的问题,如图:该网络连接图形界面中 有2个配置,其中System eth0 有对应的配置文件/etc/sysconfig/network-scripts/ifcfg-eth0,但是 ...
- AIX 命令大全
http://www.ahinc.com/aix/general.htm http://web.mit.edu/javadev/packages/Acme/ http://jparsec.codeha ...
- keil MDK编译器(V4.01)与H-JTAG的问题
前一阵子转投到MDK下面做开发,因为公司以前都用h-jtag做下载仿真工具,用的h-jtag版本为V0.9,我拿以前在ads1.2中编译成功的程序,稍作修改,在mdk下编译成功,仿真也没有问题,但下载 ...