题目来源:

  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的更多相关文章

  1. 乘风破浪:LeetCode真题_032_Longest Valid Parentheses

    乘风破浪:LeetCode真题_032_Longest Valid Parentheses 一.前言 这也是非常有意思的一个题目,我们之前已经遇到过两个这种括号的题目了,基本上都要用到堆栈来解决,这次 ...

  2. [Leetcode][Python]20: Valid Parentheses

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 20: Valid Parentheseshttps://oj.leetcod ...

  3. LeetCode 020 Valid Parentheses

    题目描述:Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']' ...

  4. 【一天一道LeetCode】#32. Longest Valid Parentheses

    一天一道LeetCode系列 (一)题目 Given a string containing just the characters '(' and ')', find the length of t ...

  5. LeetCode解题报告—— Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  6. LeetCode解题笔记 - 20. Valid Parentheses

    这星期听别人说在做LeetCode,让他分享一题来看看.试了感觉挺有意思,可以培养自己的思路,还能方便的查看优秀的解决方案.准备自己也开始. 解决方案通常有多种多样,我觉得把自己的解决思路记录下来,阶 ...

  7. leetcode problem 32 -- Longest Valid Parentheses

    Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...

  8. 【LeetCode练习题】Longest Valid Parentheses

    Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...

  9. LeetCode之“动态规划”:Valid Parentheses && Longest Valid Parentheses

    1. Valid Parentheses 题目链接 题目要求: Given a string containing just the characters '(', ')', '{', '}', '[ ...

  10. 【LeetCode】32. Longest Valid Parentheses (2 solutions)

    Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...

随机推荐

  1. mysql 主从实现

    主库:192.168.1.19 从库:192.168.1.20 开启db_test单库复制 常见问题 参考文档 主配置  以mysql root用户登录,用下面sql创建专门用于主从复制的mysql用 ...

  2. MyGui 3.2.0(OpenGL平台)的编译

    MyGui是一个用来创建用户图形界面的库,用于游戏和3D应用程序.这个库的主要目标是达到:快速.灵活.易用. 1.下载准备: 源代码:http://svn.code.sf.net/p/my-gui/c ...

  3. HBA简介及原理

    HBA,即主机总线适配器英文“Host Bus Adapter”缩写.是一个使计算机在服务器和存储装置间提供输入/输出(I/O)处理和物理连接的电路板和/或集成电路适配器. 简介 主机总线适配器(Ho ...

  4. Swift和Objective-C混合编程

    假设你现在就是一个iOS程序员,你对Objective-C很熟悉,对iOS开发也很熟悉,然而,苹果公司在iOS 8之后推出了Swift语言.那么,如何才能快速地从Objective-C过渡到Swift ...

  5. Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (四) —— ContentProvider

    ContentProvider是安卓平台中,在不同应用程序之间实现数据共享的一种机制.一个应用程序如果需要让别的程序可以操作自己的数据,即可采用这种机制.并且此种方式忽略了底层的数据存储实现,Cont ...

  6. ADO.NET知识的运用一(Day 26)

    哈哈,又到了总结的时间了,来回顾一下今天主要学了关于ADO.NET的哪些知识吧.(这次学的ADO访问数据库主要以访问SQL数据库为主) 理论:  首先我们要知道为什么要学习ADO.NET? 因为我们之 ...

  7. UITextView 限制输入字数

    尊重原创  http://blog.csdn.net/fengsh998/article/details/45421107 对于限制UITextView输入的字符数.相信大家在网上见得最多的是实现UI ...

  8. ExtJS002Window创建

    Ext.onReady(function () { Ext.create('Ext.window.Window', { title: 'window', width: 400, height: 300 ...

  9. Java如何实现对Mysql数据库的行锁

    场景如下:     用户账户有余额,当发生交易时,需要实时更新余额.这里如果发生并发问题,那么会造成用户余额和实际交易的不一致,这对公司和客户来说都是很危险的. 那么如何避免:     网上查了下,有 ...

  10. 数组有没有 length()这个方法? String 有没有 length()这 个方法?

    1.数组中有length属性. 2.String有lenth()方法.