[LeetCode 题解]: 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.
题解: 典型的STL stack 应用。注意边界条件。
class Solution {
public:
bool isValid(string s) {
stack<char> st;
if(s.size()<= || s.size()%) return false;
for(int i=;i<s.size();i++)
{
if(s[i]=='(' || s[i]=='[' || s[i]=='{') st.push(s[i]);
else if(st.empty())
{
return false;
}
else
{
if( (s[i]==')' && st.top()=='(') ||
(s[i]==']' && st.top()=='[') ||
(s[i]=='}' && st.top()=='{') )
st.pop();
else
return false;
}
}
if(!st.empty()) return false;
else return true;
}
};
转载请注明出处: http://www.cnblogs.com/double-win/ 谢谢!
[LeetCode 题解]: Valid Parentheses的更多相关文章
- leetcode 题解 || Valid Parentheses 问题
problem: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if ...
- [LeetCode] Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [LeetCode] Longest Valid Parentheses 解题思路
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [Leetcode] longest valid parentheses 最长的有效括号
Given a string containing just the characters'('and')', find the length of the longest valid (well-f ...
- [LeetCode] Longest Valid Parentheses -- 挂动态规划羊头卖stack的狗肉
(Version 1.3) 这题在LeetCode上的标签比较有欺骗性,虽然标签写着有DP,但是实际上根本不需要使用动态规划,相反的,使用动态规划反而会在LeetCode OJ上面超时.这题正确的做法 ...
- [LeetCode] 20. 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] Longest Valid Parentheses
第一种方法,用栈实现,最容易想到,也比较容易实现,每次碰到‘)’时update max_len,由于要保存之前的‘(’的index,所以space complexity 是O(n) // 使用栈,时间 ...
- [LeetCode] Longest Valid Parentheses 动态规划
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [leetcode]20. Valid Parentheses有效括号序列
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
随机推荐
- ojective-c convert to pascal pattern
ojective-c convert to pascal pattern http://www.cnblogs.com/cnsoft/archive/2013/06/09/3128619.html C ...
- manta api
Authentication 有几个访问方法. 验证对服务的请求的主要方法是使用TLS上的HTTP签名. 在大多数情况下,您只需使用SSH私钥对HTTP Date标头的小写日期:和值进行签名; 这样做 ...
- drbd mysql
使用drbd前 升级内核 yum install kernel* 重启 4.1.准备: 两个节点ha-node1和ha-node2均按照centos7.0系统,每个节点两块磁盘,一块用作根分区一块用作 ...
- Phong & BlinnPhong Specular Shader
[Phong Specular Shader] 如果物体离摄像机很远,或者不需要高精度镜面反射,则Phong模型适用. Phong模型如下: 使用前必须指定使用自定义Phong. [BlinnPhon ...
- linux: cat more less tail head查看文件内容
- 【HDU 6191】Query on A Tree 【可持久化字典树】
题目 给出一棵有n个结点的树,树根是1,每个结点给出一个value.然后给出q个询问,每个询问给出两个整数u和x,你要在以u结点为根的子树中找出一个结点v,使得val[v] xor x最大, 并输出这 ...
- Codeforces 667C DP
题意:给你一个字符串,这个字符串的构造方法如下:先选择一个长度大于4的前缀,然后每次向字符串尾部添加一个长度为2或者长度为3的后缀,不能添加连续的相同的后缀,问可能的后缀有哪些?并按字典序输出去. 思 ...
- Echarts主题颜色
Echarts主题颜色搜集: 直接覆盖默认颜色即可 例如在 echarts.setOption({ '#2ec7c9','#b6a2de','#5ab1ef','#ffb980','#d87a80', ...
- 怎样取得selected的option选项的value值
现在有一id=test的下拉框,怎么拿到选中的那个值呢? 分别使用javascript原生的方法和jquery方法 <select id="test" name=& ...
- ServiceStack.Redis泛型存储后getById问题
关于ServiceStack.Redis实体存储常用的有一下几个方法 StoreAsHash<T>(T entity) //将对象按照Hash存储 Redis.As<T>() ...