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 ...
随机推荐
- always NetWork Performance measure Tools
1,iperf key feature:Measuring TCP and UDP BandWidth Performance Iperf features; *TCP .Measure bandwi ...
- perf
- javascript里面dom操作和兼容问题汇总
DOM 增,删,改,查 oUl.children 获取UL的子节点 有length的属性 oUl.children[0] 获取UL第1个子节点 使 ...
- Gradle 1.3之前的Publishing artifacts
在Gradle1.3之前,Publishing artifacts是使用uploadConfigurationName来publish 声明artifacts是靠使用 build.gradle art ...
- 移动前端工作的那些事---前端制作篇之meta标签篇
移动端前端制作有些地方不同于互联网,这篇主要讨论的是meta标签.meta标签位于head标签之间.是主要辅助HTML结构层的.meta标签不管在互联网前端还是在移动端都起了很重要的作用.这里只讨论移 ...
- zoj3591 Nim(Nim博弈)
ZOJ 3591 Nim(Nim博弈) 题目意思是说有n堆石子,Alice只能从中选出连续的几堆来玩Nim博弈,现在问Alice想要获胜有多少种方法(即有多少种选择方式). 方法是这样的,由于Nim博 ...
- Spring REST实践之客户端和测试
RestTemplate 可参考spring实战来写这部分. RestTemplate免于编写乏味的样板代码,RestTemplate定义了33个与REST资源交互的方法,涵盖了HTTP动作的各种形式 ...
- WebService《JavaEE6权威指南 基础篇第4版》
[Web服务] 为运行在不同平台和框架之上的软件提供了互操作的标准方式.良好的互操作性和可扩展性.消息采用自包含文档的形式. ——解决异构系统之间交互.解决异构系统通信问题: 1.通过XML,JSO ...
- 关于div的居中的问题
(从已经死了一次又一次终于挂掉的百度空间人工抢救出来的,发表日期2014-01-11) div水平和垂直居中,text-align和vertical-align不起作用,因为标签div没有这两个属性, ...
- 百度语音识别REST API——通过使用Http网络请求方式获得语音识别功能
百度语音识别通过REST API的方式给开发人员提供一个通用的HTTP接口,基于该接口,开发人员能够轻松的获取语音识别能力,本文档描写叙述了使用语音识别服务REST API的方法. 长处: 较之开发人 ...