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 ...
随机推荐
- ZOJ-3201 Tree of Tree 树形DP
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3201 题意:给一颗树,每个节点有一个权值,求节点数为n的最大权子 ...
- mediawiki 的使用
首先,程序里会先加载 includes/DefaultSettings.php,然后再加载 LocalSettings.php,这样定义一些权限.其中 DefaultSettings.php 是默认的 ...
- 借助LVS+Keepalived实现负载均衡(转)
原文:http://www.cnblogs.com/edisonchou/p/4281978.html 一.负载均衡:必不可少的基础手段 1.1 找更多的牛来拉车吧 当前大多数的互联网系统都使用了服务 ...
- (mac)Android Studio安装以及Fetching android sdk component information超时的解决方案
解决Mac下面Fetching android sdk component information加载过久问题, 关于windows中可以参考前面一篇文章 关于安装和下载可以百度一下地址.安装完成后, ...
- hdoj 5386 Cover
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5386 倒着推的一个挺暴力的题,看着和数学有关系,然而并没有, 不要一看到含有数学元素就考虑这是一个数学 ...
- 建表的sql
1. 创建用户表 create table user( id int unsigned not null primary key auto_increment comment '自增id', user ...
- [SQL]sql语句bug
sql语句格式必须严格检查,一个空格的错误都会导致执行错误. 常见有 1:字符串的值要用 ‘ ’ 括起来 2:用 , 分隔语句段 3:切记判断条件前有一空格:eg:这里最容易出错 4:属性名是否 ...
- Don’t use Suspend and Resume, but don’t poll either.
http://www.paradicesoftware.com/blog/2014/02/dont-use-suspend-and-resume-but-dont-poll-either/ Don’t ...
- uva129 - Krypton Factor 7.4.3 困难的串
7.4.3困难的串 学习点:dfs加入返回值,递归搜索过程中如果有一个成功,就直接退出 //7.4.3 困难的串 #include<cstdio> #include<cstrin ...
- 正则表达式_删除字符串中的任意空格(Regex)
直接用 -split,默认以空白分隔.-split $a 用正则表达式中的 \s,-replace -split中都可以直接使用正则表达式,select-string也可以 split 和 join ...