[LeetCode]题解(python):020-Valid Parentheses
题目来源:
https://leetcode.com/problems/valid-parentheses/
题意分析:
这道题输入一段只包括括号的字符串,判断这个字符串是否已经配对。配对的规则是,每个'(' 和一个 ')'配对,每个'[' 和一个 ']'配对,每个'{' 和一个 '}' 配对,左括号先出现,并且他们之间的字符串也是配对的。比如说“()[]”两个小括号可以配对,他们之间字符串为空可以配对,两个中括号也如此;所以他是配对的。而“([)]”,虽然小括号和中括号都有配对,不过小括号之间只有一个中括号,一个中括号不能配对;所以他不能配对。
题目思路:
当我们面对一串括号的字符串,首先我们人工的做法是先把确定可以配对的,也就是括号间没有其他字符的先配对,然后去掉,剩下的继续用这种方法去做。要用程序来实现这个过程,我们可以利用堆栈来做,也就是C++里面的stack。如果遇到左括号,我们就将括号push到一个stack里面,如果遇到右括号,那么将stack的队尾pop出,比较是否可以配对,如果可以,继续,如果不可以,返回False。在python里面list也可以当作stack来用。只不过push变成了append。
代码(python):
class Solution(object):
def match(self,s1,s2):
if s1 == '(' and s2 ==')':
return True
if s1 == '[' and s2 ==']':
return True
if s1 == '{' and s2 =='}':
return True
return False
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
ans = []
for i in s:
if i == '(' or i == '{' or i == '[':
ans.append(i)
if i == ')' or i == ']' or i == '}':
if len(ans) == 0:
return False
tmp = ans.pop()
if not self.match(tmp,i):
return False
if len(ans) == 0:
return True
return False
转载请注明出处:http://www.cnblogs.com/chruny/p/4850436.html
[LeetCode]题解(python):020-Valid Parentheses的更多相关文章
- 乘风破浪:LeetCode真题_032_Longest Valid Parentheses
乘风破浪:LeetCode真题_032_Longest Valid Parentheses 一.前言 这也是非常有意思的一个题目,我们之前已经遇到过两个这种括号的题目了,基本上都要用到堆栈来解决,这次 ...
- [Leetcode][Python]20: Valid Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 20: Valid Parentheseshttps://oj.leetcod ...
- LeetCode 020 Valid Parentheses
题目描述:Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']' ...
- 【一天一道LeetCode】#32. Longest Valid Parentheses
一天一道LeetCode系列 (一)题目 Given a string containing just the characters '(' and ')', find the length of t ...
- LeetCode解题报告—— Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- LeetCode解题笔记 - 20. Valid Parentheses
这星期听别人说在做LeetCode,让他分享一题来看看.试了感觉挺有意思,可以培养自己的思路,还能方便的查看优秀的解决方案.准备自己也开始. 解决方案通常有多种多样,我觉得把自己的解决思路记录下来,阶 ...
- leetcode problem 32 -- Longest Valid Parentheses
Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...
- 【LeetCode练习题】Longest Valid Parentheses
Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...
- LeetCode之“动态规划”:Valid Parentheses && Longest Valid Parentheses
1. Valid Parentheses 题目链接 题目要求: Given a string containing just the characters '(', ')', '{', '}', '[ ...
- 【LeetCode】32. Longest Valid Parentheses (2 solutions)
Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...
随机推荐
- JPEGOPTIM压缩优化
有关JPEGOPTIM JPEGOPTIM是CDN供应商Akamai开发的一个图片人优化的开源小工具. 它有较好的图片压缩效果(压缩比.图片质量), 比PHP的GD库算法要好. 详细的介绍可以看这个文 ...
- 如何解决dns解析故障
在实际应用过程中可能会遇到DNS解析错误的问题,就是说当我们访问一个域名时无法完成将其解析到IP地址的工作,而直接输入网站IP却可以正常访问,这就是因为DNS解析出现故障造成的.这个现象发生的机率比较 ...
- OpenStack安装部署管理中常见问题解决方法
一.网络问题-network 更多网络原理机制可以参考<OpenStack云平台的网络模式及其工作机制>. 1.1.控制节点与网络控制器区别 OpenStack平台中有两种类型的物理节点, ...
- oracle slient静默安装并配置数据库及仅安装数据库不配置数据库shell
<1,>仅安装数据库软件不配置数据库 ./x86oracle.sh /ruiy/ocr/DBSoftware/app/oracle /ruiy/ocr/DBSoftware/app/ora ...
- .Net Web开发中实现剪切板功能
我要实现的功能是:在列表页,通过一个按钮复制对应的文章Url,如下图: 如下代码: <a class="btn btn-success copy" href=&quo ...
- sql表连接的几种方式
这里有两张表TableA和TableB,分别是姓名表和年龄表,用于我们例子的测试数据 TableA id name 1 t1 2 t2 4 t4 TableB id age 1 18 2 20 3 1 ...
- BZOJ 1083: [SCOI2005]繁忙的都市(MST)
裸的最小生成树..直接跑就行了 ---------------------------------------------------------------------- #include<c ...
- codility上的练习(5)
codility出了lesson 5了. (1) 合法括号序列,包括( [ { ) ] }这6种字符的字符串,长度N在[0..200000]范围内,为其是否合法. 要求时间复杂度O(N),空间复杂度O ...
- [转]如何理解c和c ++的复杂类型声明
本文作者girlrong是网易广州社区的C语言版版主,这篇文章被选在精华区.很是不错,不敢独享!据说她乐于助人,虚心诚恳,颇受网友欢迎.只可惜现在已退隐江湖了.在最近学习C语言过程中,了解些前辈大牛的 ...
- include file和include virtual的区别
1.#include file 包含文件的相对路径,#include virtual包含文件的虚拟路径. 2.在同一个虚拟目录内,<!--#include file="file.asp ...