【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 ...
随机推荐
- go实现生产者消费者
package main import ( "fmt" "math/rand" ) func main() { ch := make(chan int) don ...
- Bootstrap基础知识学习
Bootstrap中文网 http://www.bootcss.com/ Bootstrap菜鸟教程 http://www.runoob.com/bootstrap/bootstrap-tutoria ...
- JavaScript中,关于class的调用
PS:class的调用,其实是可以叠加的,当然了这要求样式不同的情况下,如果样式相同,则后一个样式会覆盖前一个样式. 1.举例如下: <div id="test" class ...
- PHP小数处理常用函数
1.php保留两位小数并且四舍五入 $num = 123213.666666; echo sprintf("%.2f", $num); // 123213.67echo round ...
- C语言中的二级指针(双指针)
原创作品,转载请标明出处http://blog.csdn.net/yming0221/article/details/7220688 C语言更多查看 C语言使用注意事项(一) C语言使用注意事项(二) ...
- ios项目icon和default图片命名规则
一.应用图片标准iOS控件里的图片资源,苹果已经做了相应的升级,我们需要操心的是应用自己的图片资源.就像当初为了支持iPhone 4而制作的@2x高分辨率版本(译者:以下简称高分)图片一样,我们要为i ...
- Objective-C分类 (category)和扩展(Extension) 的区别
http://blog.csdn.net/yhawaii/article/details/6992094 http://blog.163.com/wangy_0223/blog/static/4501 ...
- win10搭建Java环境
一.下载地址 jdk和jre官方网址:http://www.oracle.com/technetwork/java/javase/downloads/index.html 根据你的系统选择你需要 ...
- 多进程Queue
进程间通讯 不同进程间内存是不共享的,要想实现两个进程间的数据交换,可以用以下方法: Queues 使用方法跟threading里的queue差不多 from multiprocessing impo ...
- go语音实战读后感——一
1.第一个go程序: package main import ( "fmt" ) func main() { fmt.Println("Hello go") } ...