这道题是LeetCode里的第20道题。

题目要求:

给定一个只包括 '('')''{''}''['']' 的字符串,判断字符串是否有效。

有效字符串需满足:

  1. 左括号必须用相同类型的右括号闭合。
  2. 左括号必须以正确的顺序闭合。

注意空字符串可被认为是有效字符串。

示例 1:

输入: "()"
输出: true

示例 2:

输入: "()[]{}"
输出: true

示例 3:

输入: "(]"
输出: false

示例 4:

输入: "([)]"
输出: false

示例 5:

输入: "{[]}"
输出: true

这还用想吗?堆栈无脑解题!!!左括号进栈,右括号判断并出栈。

题解代码:

class Solution {
public:
bool isValid(string s) {
stack<char> match;
char c;
for(int i=0;i<s.length();i++){
if(!match.size())
match.push(s[i]);
else{
c=match.top();
if(c=='('&&s[i]==')'||c=='['&&s[i]==']'||c=='{'&&s[i]=='}')
match.pop();
else{
if(s[i]==')'||s[i]==']'||s[i]=='}')
return false;
else
match.push(s[i]);
}
}
}
if(!match.size())
return true;
return false;
}
};

提交结果:

个人总结:

啥都没有,但是得贴个 python 的解法:

class Solution:
def isValid(self, s):
while '{}' in s or '()' in s or '[]' in s:
s = s.replace('{}', '')
s = s.replace('[]', '')
s = s.replace('()', '')
return s == ''

python 真是简洁啊!

【LeetCode】Valid Parentheses(有效的括号)的更多相关文章

  1. [LeetCode] Valid Parentheses 验证括号

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  2. [Leetcode] valid parentheses 有效括号对

    Given a string containing just the characters'(',')','{','}','['and']', determine if the input strin ...

  3. leetCode 20.Valid Parentheses (有效的括号) 解题思路和方法

    Valid Parentheses  Given a string containing just the characters '(', ')', '{', '}', '[' and ']', de ...

  4. [LeetCode] Valid Parenthesis String 验证括号字符串

    Given a string containing only three types of characters: '(', ')' and '*', write a function to chec ...

  5. LeetCode: Valid Parentheses 解题报告

    Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', det ...

  6. 【LeetCode】20. Valid Parentheses 有效的括号

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:有效,括号,括号匹配,栈,题解,leetcode, 力扣 ...

  7. LeetCode OJ:Valid Parentheses(有效括号)

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  8. [LeetCode]20. Valid Parentheses有效的括号

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  9. 【LeetCode】1111. Maximum Nesting Depth of Two Valid Parentheses Strings 有效括号的嵌套深度

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目讲解 划分规则讲解 返回结果讲解 解题方法 代码 日期 题目地址:ht ...

随机推荐

  1. 将Object转换成Dictionary方法

    如果Object是Dictionary类型,直接返回 如果Object是NameValueCollection类型,则添加到Dictionary里 如果Object是Hashtable类型,添加到Di ...

  2. python部分 + 数据库 + 网络编程

    PS:附上我的博客地址,答案中略的部分我的博客都有,直接原标题搜索即可.https://www.cnblogs.com/Roc-Atlantis/ 第一部分 Python基础篇(80题) 为什么学习P ...

  3. 初识requestAnimationFrame

    转载地址:https://blog.csdn.net/vhwfr2u02q/article/details/79492303 核心概念: 1.CPU节能:在页面不刷新时不执行回调(页面在隐藏.最小化等 ...

  4. 声明已被否决 VS C++

    error C4996声明已被否决,不止一次碰到这个问题,在这里必须mark一下! 尝试这个1.Project Properties > Configuration Properties > ...

  5. iphone之打开pdf、doc、xls文件用UIWebView

    //文件名字及类型 NSString *path=[[NSBundle mainBundle]pathForResource:@"xls1" ofType:@"xls&q ...

  6. 中国区 Azure 应用程序开发说明

    1.文档简介 微软公司为其在境外由微软运营的 Azure 服务(以下简称为 “境外 Azure”),创建和部署云应用程序,提供了相应工具. 在中国,由世纪互联运营的 Microsoft Azure ( ...

  7. Linux OpenGL 实践篇-15-图像数据操作

    OpenGL图像数据操作 之前的实践中,我们在着色器中的输入输出都是比较固定的.比如在顶点或片元着色器中,顶点属性的输入和帧缓存的颜色值:虽然我们可以通过纹理或者纹理缓存对象(TBO)来读取任意的内存 ...

  8. [置顶] IIS应用程序池多工作进程设置及Session共享

    [置顶] IIS应用程序池多工作进程设置及Session共享   在调优iis的时候,朋友分享给我一个特别棒的设置方法步骤,感谢好朋友的分享. IIS应用程序池多工作进程设置及Session共享 1  ...

  9. hibernate3缓存(hibernate)

    一级缓存:当应用程序调用Session 的save() .update() .savaeOrUpdate() .get() 或load() ,以及调用查询接口的list() .iterate() 或f ...

  10. PAT 乙级 1027

    题目 题目地址:PAT 乙级 1027 思路 本题需要注意两点: 1. 对于每行输出字符的循环和判断没有完全搞清楚,导致在4 * 的条件下会输出7个字符,n的结果是-3. 2. 没有考虑到小于等于0的 ...