【leetcode❤python】 20. Valid Parentheses
#-*- coding: UTF-8 -*-
#利用栈的思想
#如果输入的左测扩则入栈,如果输入为右侧扩,判断其是否与当前栈顶配对,如果匹配则删除这一对,否则return False
#'(', ')', '{', '}', '[' and ']'
class Solution(object):
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
left_tag=['(','{','[']
right_tag=[')','}',']']
stack_list=[]
match_dic={'(':')','{':'}','[':']'}
for i in range(len(s)):
curcheck=s[i]
if left_tag.__contains__(curcheck):
stack_list.append(curcheck)
elif right_tag.__contains__(curcheck):
if len(stack_list)!=0 and match_dic.get(stack_list[-1])==curcheck:
stack_list.pop()
else:
return False
else:return False
return True if len(stack_list)==0 else False
sol=Solution()
print sol.isValid('[')
【leetcode❤python】 20. Valid Parentheses的更多相关文章
- 【LeetCode练习题】Longest Valid Parentheses
Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...
- 【leetcode❤python】 125. Valid Palindrome
#-*- coding: UTF-8 -*- class Solution(object): def isPalindrome(self, s): ""&quo ...
- 【leetcode❤python】 36. Valid Sudoku
#-*- coding: UTF-8 -*-#特定的九个格内1-9的个数至多为1#依次检查每行,每列,每个子九宫格是否出现重复元素,如果出现返回false,否则返回true.class Solutio ...
- 【leetcode❤python】242. Valid Anagram
class Solution(object): def isAnagram(self, s, t): if sorted(list(s.lower()))==sorted(list ...
- LeetCode解题笔记 - 20. Valid Parentheses
这星期听别人说在做LeetCode,让他分享一题来看看.试了感觉挺有意思,可以培养自己的思路,还能方便的查看优秀的解决方案.准备自己也开始. 解决方案通常有多种多样,我觉得把自己的解决思路记录下来,阶 ...
- leetCode练题——20. Valid Parentheses
1.题目 20. Valid Parentheses——Easy Given a string containing just the characters '(', ')', '{', '}', ...
- 【leetcode❤python】Sum Of Two Number
#-*- coding: UTF-8 -*- #既然不能使用加法和减法,那么就用位操作.下面以计算5+4的例子说明如何用位操作实现加法:#1. 用二进制表示两个加数,a=5=0101,b=4=0100 ...
- [Leetcode][Python]32: Longest Valid Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 32: Longest Valid Parentheseshttps://oj ...
- 【LeetCode】20. Valid Parentheses 有效的括号
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:有效,括号,括号匹配,栈,题解,leetcode, 力扣 ...
随机推荐
- 自动开票-不能获取汇款地址 Cannot get remit to address
1. Cannot get remit to address 1. 查看客户Bill-to Address的Country信息; 2. 选择Receivable Manager职责,通过路径Setup ...
- vim多行缩进的方法
在visual模式下选中要缩进的行,然后按>
- Openstack的HA解决方案【haproxy和keepalived】
1. 安装haproxy,keepalived, httpd,3台机器一致. yum install haproxy keepalived httpd -y 2. 修改httpd的默认页面. 在/va ...
- centos 6 7更改主机名 命令添加ip
6:vi /etc/sysconfig/network 中hostname =主机名 vi /etc/hosts 添加127.0.0.1相应的主机名 7: hostnamectl set-hostn ...
- zjtd 2016面试
1.写一个函数get_next() class A{ public :int next(); //取下一个值,并且指针后移 bool has_next(); private: //可以认为是一个q ...
- HTTP 请求未经客户端身份验证方案“Anonymous”授权。从服务器收到的身份验证标头为“Negotiate,NTLM”
转自:http://www.cnblogs.com/geqinggao/p/3270499.html 近来项目需要Web Service验证授权,一般有两种解决方案: 1.通过通过SOAP Heade ...
- [转]System.Reflection.AssemblySignatureKeyAttribute
转自:http://www.cnblogs.com/ego/p/3321122.html 错误: Could not load type 'System.Reflection.AssemblySign ...
- vs2010 仿XCode风格的头注释宏
Sub DocumentFileHeader() Dim star As String star = "//***************************************** ...
- 【jQuery UI 1.8 The User Interface Library for jQuery】.学习笔记.9.Progressbar控件
Progressbar控件用来显示任意进程的完成百分比. 默认安装启用 配置选项 控件暴露的事件API progressbar暴露的独一无二的方法 一些现实生活的例子 当前版本中,我们或系统必须明确进 ...
- 通达OA 同步中控考勤机 增强版
如果你用的是中控考勤机且考勤机能联网,那恭喜有福了! 最近发现考勤机提供web方式查询,经过调试可以用程序直接读取考勤机数据跨过考勤机软件及其access数据库,数据同步及时性.可靠性大幅提高. 通达 ...