题目来源:

  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. php mvc 框架演示

    <pre name="code" class="cpp"><pre name="code" class="pyt ...

  2. linux shell编程总结

    linux shell编程总结 本周学习了unix/linux shell编程,参考的是<LINUX与UNIX Shell 编程指南>,David Tansley著:徐焱,张春萌等译,由机 ...

  3. oracle 11g RAC ocfs2

    http://oracle-base.com/articles/linux/ocfs2-on-linux.php http://oracle-base.com/articles/11g/oracle- ...

  4. gdbserver 安卓apk

    gdbserver  调试程序 底层调用c/c++ 动态库, 动态库带调试选项 查看手机IP 192.168.1.177 包所调用的c/c++ 库是在/data/data/包名/lib/ 下 1.将安 ...

  5. Android学习路线(二十)运用Fragment构建动态UI

    要在Android系统上创建一个动态或者多面板的用户界面,你须要将UI组件以及activity行为封装成模块.让它可以在你的activity中灵活地切换显示与隐藏. 你可以使用Fragment类来创建 ...

  6. linux c 通过文件描写叙述符获取文件名称

    在linux中每一个被打开的文件都会在/proc/self/fd/文件夹中有记录,当中(/proc/self/fd/文件描写叙述符号:这个文件是符号文件)的文件就是文件描写叙述符所相应的文件. 而re ...

  7. No.1小白的HTML+CSS心得篇

    一个web前端的小白,听前辈说写好笔记很关键,so 特此用博客来开始记录自己的旅程——Web之路 最近几天看的HTML 1.纠正一个认知错误 “HTML是一种编程语言”  ————(错) HTML ( ...

  8. Navicat Premium 11.0.x(for Mac)激活方法

    激活步骤: 1.将解压后的Navicat Premium.app放入Applications(应用目录),但一定不要打开它2.彻底断网3.将keygen.app复制到桌面->右键->Get ...

  9. C++函数声明和定义深度解析

    概述: 声明是将一个名称引入一个程序. 定义提供了一个实体在程序中的唯一描述. 声明在单个作用域内可以重复多次(类成员除外),定义在一个给定的作用域内只能出现一次. 一个定义就是一个声明,除非: 它定 ...

  10. python质量控制

    一种编写高质量软件的方式是给代码中每个函数写测试,在开发过程中经常性的进行测试.         doctest模块可以在docstring中嵌套测试代码.例如: def average(values ...