【LeetCode】Valid Parentheses(有效的括号)
这道题是LeetCode里的第20道题。
题目要求:
给定一个只包括
'(',')','{','}','[',']'的字符串,判断字符串是否有效。有效字符串需满足:
- 左括号必须用相同类型的右括号闭合。
- 左括号必须以正确的顺序闭合。
注意空字符串可被认为是有效字符串。
示例 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(有效的括号)的更多相关文章
- [LeetCode] Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [Leetcode] valid parentheses 有效括号对
Given a string containing just the characters'(',')','{','}','['and']', determine if the input strin ...
- leetCode 20.Valid Parentheses (有效的括号) 解题思路和方法
Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', de ...
- [LeetCode] Valid Parenthesis String 验证括号字符串
Given a string containing only three types of characters: '(', ')' and '*', write a function to chec ...
- LeetCode: Valid Parentheses 解题报告
Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', det ...
- 【LeetCode】20. Valid Parentheses 有效的括号
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:有效,括号,括号匹配,栈,题解,leetcode, 力扣 ...
- LeetCode OJ:Valid Parentheses(有效括号)
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [LeetCode]20. Valid Parentheses有效的括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- 【LeetCode】1111. Maximum Nesting Depth of Two Valid Parentheses Strings 有效括号的嵌套深度
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目讲解 划分规则讲解 返回结果讲解 解题方法 代码 日期 题目地址:ht ...
随机推荐
- JavaScript中,关于class的调用
PS:class的调用,其实是可以叠加的,当然了这要求样式不同的情况下,如果样式相同,则后一个样式会覆盖前一个样式. 1.举例如下: <div id="test" class ...
- 浅谈C++ 内存泄漏及其检测工具
浅谈C++ 内存泄漏及其检测工具 http://wenku.baidu.com/link?url=1DGkOOvd_ITZyB8IHAwfhCOx2tfO6id8UfuyQkAMHZU6sasaAXz ...
- Android WiFi使用记录
最近在做Android的WiFi部分的开发,连接的工具类参照了这个文章的工具类. http://www.cnblogs.com/zhuqiang/p/3566686.html 开发中碰上的一些问题,在 ...
- npm install -g cnpm --registry=https://registry.npm.taobao.org
npm install -g cnpm --registry=https://registry.npm.taobao.org
- Android(java)学习笔记145:Handler消息机制的原理和实现
联合学习 Android 异步消息处理机制 让你深入理解 Looper.Handler.Message三者关系 1. 首先我们通过一个实例案例来引出一个异常: (1)布局文件activity_m ...
- git快速入门(MAC系统,github,ssh key)
如果使用过svn的话,git大致可以认为是多了本地库的svn.git先本地提交commit到本地库,然后再push到远程服务器的库.git是分布式的代码管理工具,基于SSH协议.ssh的作用就是为了不 ...
- URL URI URN的区别
下面这张图可以完美的解释他们三者之间的关系 URI包含URL和URN Uniform Resource Identifier :统一资源标志符,用于标识某一互联网资源 Uniform Resoutce ...
- plsql循环的简单实例
declare v_id tbl_regions.regions_id%type; begin .. loop select t.regions_id into v_id from tbl_regio ...
- SpringMVC+Spring+Mybatis整合程序之整合
因为每个人思路不一样,所以我在这边先分享自己的思路对于mybatis开发持久层(DAO:DataBase Access Object 持久层访问对象)有两种.第一种:传统的开发持久层方式即需要程序员开 ...
- JsonUtils工具类
public class JsonUtils { public static void printTimeObject(Object obj, HttpServletResponse response ...