[LintCode] 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.
LeetCode上的原题,请参见我之前的博客Valid Parentheses。
class Solution {
public:
/**
* @param s A string
* @return whether the string is a valid parentheses
*/
bool isValidParentheses(string& s) {
stack<char> st;
for (char c : s) {
if (c == ')' || c == '}' || c == ']') {
if (st.empty()) return false;
if (c == ')' && st.top() != '(') return false;
if (c == '}' && st.top() != '{') return false;
if (c == ']' && st.top() != '[') return false;
st.pop();
} else {
st.push(c);
}
}
return st.empty();
}
};
[LintCode] Valid Parentheses 验证括号的更多相关文章
- [LeetCode] 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] 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] valid parentheses 有效括号对
Given a string containing just the characters'(',')','{','}','['and']', determine if the input strin ...
- LintCode: Valid Parentheses
C++ stack<char|int|string>, push(), pop(), top(), empty(), size() class Solution { public: /** ...
- 20. Valid Parentheses检验括号字符串的有效性
[抄题]: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if th ...
- 2.Valid Parentheses (括号匹配)
Level: Easy 题目描述: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
- [LintCode] Valid Number 验证数字
Validate if a given string is numeric. Have you met this question in a real interview? Yes Example & ...
随机推荐
- windows多线程详解
转自:http://blog.csdn.net/zhouxuguang236/article/details/7775232 在一个牛人的博客上看到了这篇文章,所以就转过来了,地址是http://bl ...
- Mysql数据库操作系统及配置参数优化
数据库结构优化 表的水平拆分常用的水平拆分方法为:1.对 customer_id进行 hash运算,如果要拆分成5个表 则使用mod(customer_id,5)取出0-4个值2.针对不同的 hash ...
- 关于html中的设置body宽高的理解
有时候看到别人的代码中经常出现在body中设置的宽高,不是很理解,于是自己测试了下,瞬间懂了,废话不多说,直接上代码: 首先创建好一个基本的html文件,设body的背景色为red: 相信大家都知道效 ...
- 【tomcat 无法部署】svn上下载的maven项目无法部署到tomcat中
问题: svn上下载的maven项目无法部署到tomcat中,tomcat不识别项目,但是这个项目确实是web项目 发现的过程: 然后依次产看项目的编译版本: 项目的依赖架包: 才发现: 解决方法: ...
- HDU 4858 项目管理 分块
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4858 题解: 下面说一个插入查询时间复杂度为sqrt(m)的算法: 对每个点定义两个值:val,su ...
- 提取Windows用户密钥文件cachedump
提取Windows用户密钥文件cachedump Windows系统将用户信息和密钥存储在系统存档文件(System hive)和安全存档(Security hive)中.只要提取这些内容,就可以 ...
- 并查集(路径更新) LA 3027 Corporative Network
题目传送门 题意:训练指南P192 分析:主要就是一个在路径压缩的过程中,更新点i到根的距离 #include <bits/stdc++.h> using namespace std; c ...
- HDU5816 Hearthstone(状压DP)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5816 Description Hearthstone is an online collec ...
- myeclipse操作记录
myeclipse里面jsp页面里面js注释快捷键 ctrl + shift +cctrl + / MyEclipse 每次保存都要building workspace的解决方法 方法一:菜单栏-&g ...
- Unity Standard Assets 简介之 Utility
决定写这个系列起因于刚才的偶然所见,我发现当前版本的Unity有一个 Utility 标准资源包,里面有一个 FPSCounter 脚本提供了显示游戏运行帧率的基本功能,而我几周前刚刚写过一个同样功能 ...