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 ...
随机推荐
- F2063 Could not compile used unit 'tt.pas'
install packge error F2063 Could not compile used unit 'tt.pas' 有可能是工程的pas文件相对路径不对.在工程管理看是否能打开文件,如果打 ...
- delphi AES encrypt
xe8 ok unit TntLXCryptoUtils; interface function AES128_Encrypt( Value, Password : string ) : string ...
- Linux下的cut选取命令详解
定义 正如其名,cut的工作就是“剪”,具体的说就是在文件中负责剪切数据用的.cut是以每一行为一个处理对象的,这种机制和sed是一样的 剪切依据 cut命令主要是接受三个定位方法: 第一,字节(by ...
- windos系统快捷键 2015-05-08 23:31 24人阅读 评论(0) 收藏
WIN7的向上按钮消失了,但是它的快捷键没有消失: Alt + ↑: 文件夹的后退前进 Alt +← 和Alt →: 切换到上个操作的窗口Alt +Esc: 版权声明:本文为博主原创文章,未经博主允许 ...
- UVALive 5886 The Grille (模拟)
The Grille 题目链接: http://acm.hust.edu.cn/vjudge/problem/26634 Description http://7xjob4.com1.z0.glb.c ...
- Terrain & Light & Camera
[Terrain Engine] 1.When you press F, wherever your mouse is positioned will be moved to the center o ...
- JavaScript面向对象简介
JavaScript面向对象简介 @(编程) [TOC] 1. 命名空间 命名空间是一个容器,它允许开发人员在一个独特的,特定于应用程序的名称下捆绑所有的功能. 在JavaScript中,命名空间只是 ...
- Window服务初级教程以及log4net配置文件初始化
Window服务初级教程:http://www.jb51.net/article/48987.htm 另外,配置log4net这个日志功能的时候需要初始化,不然会报没有初始化的错误,而且初始化的节点应 ...
- LA4329 Ping pong(树状数组与组合原理)
N (3N20000)ping pong players live along a west-east street(consider the street as a line segment). E ...
- UI:触摸事件 与 事件的回应
事件分类:晃动.触摸.远程控制(如遥控器.红外控制) 触摸开始时候的方法(判断单击,双击,三击事件可以写在这里) -(void)touchesBegan:(NSSet *)touches withEv ...