题目来源:

  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. WPF:在XmlDataProvider上使用主-从绑定(Master-Detail Binding)

    原文 http://www.cnblogs.com/mgen/archive/2011/06/19/2084553.html 示例程序: 如上程序截图,一目了然典型的主从模式绑定应用,如果里面的数据不 ...

  2. android 背景透明度渐变动画

    button.setVisibility(View.VISIBLE); // 背景透明度渐变动画 ObjectAnimator alpha = ObjectAnimator.ofFloat(butto ...

  3. 10个必备的移动UI设计资源站

    http://www.uisdc.com/10-necessary-mobile-ui-design-resources# 交互设计中如何增加趣味性.提升愉悦http://www.uisdc.com/ ...

  4. jQuery Lint: enables you to automatically inject jQuery Lint into the page as it is loaded (great for ad-hoc code validation)

    FireQuery is a Firebug extension for jQuery development jQuery Lint: enables you to automatically in ...

  5. [Android] 使用Webview进行OAUTH

    1. 源起     最近在弄Google登录,Google登录要求手机上必须按照Google Play Service,有些手机比如小米,没有Google Play Servcie,因此,有必要实现一 ...

  6. MD5的加密和解密(总结)

    效果图例如以下: package com.test; import java.security.MessageDigest; public class MD5 { // MD5加码.32位 publi ...

  7. linux下curl编程

    LibCurl是免费的客户端URL传输库,支持FTP,FTPS, HTTP, HTTPS, SCP, SFTP, TFTP, TELNET, DICT, FILE ,LDAP等协议,其主页是http: ...

  8. hibernate -inverse

    one to many inverse=false只能设置维护关联关系的多的一方, inverse属性: 默认为false,表示本方维护关联关系. 如果为true,表示本方不维护关联关系(并不意味着对 ...

  9. c++ primer plus 习题答案(2)

    p221.8 #include<iostream> #include<cstdlib> #include<cstring> using namespace std; ...

  10. Hibernate学习之hibernate状态

    hibernate有三种状态,transient(瞬时状态),persistent(持久化状态)以及detached(离线状态),瞬时状态就是刚new出来一个对象,还没有被保存到数据库中,持久化状态就 ...