LeetCode20 Valid Parentheses
题意:
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not. (Easy)
这道题是8月5号做的,居然没有写博客当时...最近真是乱了,顺便整理了一下做题日志...
分析:
比较简单,弄一个栈,左括号压栈,右括号匹配了就弹栈,不匹配或没有元素了return false, 一遍走完了判断栈是否为空。
class Solution {
public:
bool isValid(string s) {
if (s.size() == ) {
return true;
}
stack<char> sta;
sta.push(s[]);
for (int i = ; i < s.size(); ++i) {
if (s[i] == '(' || s[i] == '[' || s[i] == '{') {
sta.push(s[i]);
continue;
}
if ( (s[i] == ')' || s[i] == ']' || s[i] == '}') && sta.empty() ) { // "()]"
return false;
}
if (s[i] == ')') {
if (sta.top() == '(') {
sta.pop();
continue;
}
else {
return false;
}
}
if (s[i] == ']') {
if (sta.top() == '[') {
sta.pop();
continue;
}
else {
return false;
}
}
if (s[i] == '}') {
if (sta.top() == '{') {
sta.pop();
continue;
}
else {
return false;
}
}
return false;
}
if (sta.empty()) {
return true;
}
else {
return false;
}
}
};
第一次提交的时候忘了sta.empty()也return false 的情况;
其次,代码冗余太多,写的有点长,优化一下。
class Solution {
public:
bool isValid(string s) {
if (s.size() == ) {
return true;
}
stack<char> sta;
sta.push(s[]);
for (int i = ; i < s.size(); ++i) {
if (s[i] == '(' || s[i] == '[' || s[i] == '{') {
sta.push(s[i]);
continue;
}
else {
if ( sta.empty() ) { // "()]"
return false;
}
if (s[i] == ')' && sta.top() != '(') {
return false;
}
if (s[i] == ']' && sta.top() != '[') {
return false;
}
if (s[i] == '}' && sta.top() != '{') {
return false;
}
sta.pop();
}
}
return sta.empty();
}
};
LeetCode20 Valid Parentheses的更多相关文章
- LeetCode 20. 有效的括号(Valid Parentheses)
20. 有效的括号 20. Valid Parentheses 题目描述 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须 ...
- [LeetCode] Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [LeetCode] Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 72. Generate Parentheses && Valid Parentheses
Generate Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
- leetcode 32. Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 【leetcode】Longest Valid Parentheses
Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...
- 【leetcode】 Longest Valid Parentheses (hard)★
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [LintCode] Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
随机推荐
- HDU ACM 3177 Crixalis's Equipment
Crixalis's Equipment Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- Hadoop MapReduce概念学习系列之mr程序组件全貌(二十)
其实啊,spilt是,控制Apache Hadoop Mapreduce的map并发任务数,详细见http://www.cnblogs.com/zlslch/p/5713652.html map,是m ...
- 【转】使用json-lib进行Java和JSON之间的转换
原文链接:http://www.cnblogs.com/mailingfeng/archive/2012/01/18/2325707.html 1. json-lib是一个java类库,提供将Java ...
- Struts Hello World Example
In this tutorial we show you how to develop a hello world web application using classic Struts 1.3 f ...
- 用jmap分析java程序
之前的随笔提到用jstack分析java线程情况,也是在这个项目中,当线程的问题解决之后,发现程序的内存一直增长,于是用jmap工具分析了一下java程序占用内存的情况. 命令很简单,直接 jmap ...
- 安装glibc2.7
参考链接http://fhqdddddd.blog.163.com/blog/static/18699154201401722649441/ /lib/libc.so.6: version `glib ...
- LC串联谐振回路
- AutoCAD.NET 不使用P/Invoke方式调用acad.exe或accore.dll中的接口(如acedCommand、acedPostCommand等)
使用C#进行AutoCAD二次开发,有时候由于C#接口不够完善,或者低版本AutoCAD中的接口缺少,有些工作不能直接通过C#接口来实现,所以需要通过P/Invoke的方式调用AutoCAD的其他DL ...
- MVC个人认为的终极分页
//传入要查询的字段,查询条件(例如根据姓名查看数据的数据筛选),按照什么排序,页码,信息条数 //T:要操作的类型 //Tkey:根据什么类型来排,ID的话返回的是int类型,但是name的话又会返 ...
- hdu 4115 Eliminate the Conflict ( 2-sat )
Eliminate the Conflict Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...