[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 ...
随机推荐
- kbengine简单介绍(1)
什么是kbengine? 一款开源的游戏服务端引擎,客户端通过简单的约定协议就能与服务端通讯, 使用KBEngine插件能够快速与(Unity3D, OGRE, Cocos2d-x, HTML5, 等 ...
- 那些年我们写过的三重循环----CodeForces 295B Greg and Graph 重温Floyd算法
Greg and Graph time limit per test 3 seconds memory limit per test 256 megabytes input standard inpu ...
- 【deep learning学习笔记】Recommending music on Spotify with deep learning
主要内容: Spotify是个类似酷我音乐的音乐站点.做个性化音乐推荐和音乐消费.作者利用deep learning结合协同过滤来做音乐推荐. 详细内容: 1. 协同过滤 基本原理:某两个用户听的歌曲 ...
- c语言中-----分配内存函数
原型: void * realloc(void *p, size_t size); realloc 可以对给定的指针所指的空间进行扩大 或者 缩小, 原有内存的数据保持不变.当然,对于缩小,则缩小部 ...
- c 转置字符串You are a so cheap man ->man cheap so a are You
解题思路: 1.将字符串转置 2.对转置后的字符串中单词转置 #include<stdio.h> #include<string.h> #include<stdlib.h ...
- js调试若干
主要是将 chrome调试工具 firebug的控制台对以下都有支持 consoleAPI https://developers.google.com/chrome-developer-tools ...
- c#学习心得,慢慢添加,如果有错误希望大家留言,我刚开始学
1.class类:相当于整个项目的一个功能性程序,为了阐述系统中某个对象的功能. 方法:相当于程序的一个功能部件.可以被其他方法或类调用?感觉这个问题有点复杂 c#框架结构:我目前接触到的 using ...
- window 7 改变窗口颜色
对于刚刚重新安装的window7系统的盆友来说,不能改变窗口的颜色,和别人的window7窗口颜色和样子不一样和不好玩,那么我教大家,如何更改窗口颜色. 首先,重装的系统用软件激活之后,肯定还没还得急 ...
- C#线程(一)
本篇作为自己简要的笔记和整理,自己英语略渣 就不翻译了 参考http://www.albahari.com/threading 这里有翻译的http://www.cnblogs.com/miniwi ...
- java思考题
1.仔细阅读示例: EnumTest.java,运行它,分析运行结果? 你能得到什么结论?你掌握了枚举类型的基本用法了吗? public class EnumTest { public static ...