[LeetCode] Valid Parentheses 验证括号
Given a string containing just the characters '('
, ')'
, '{'
, '}'
, '['
and ']'
, determine if the input string is valid.
An input string is valid if:
- Open brackets must be closed by the same type of brackets.
- Open brackets must be closed in the correct order.
Note that an empty string is also considered valid.
Example 1:
Input: "()"
Output: true
Example 2:
Input: "()[]{}"
Output: true
Example 3:
Input: "(]"
Output: false
Example 4:
Input: "([)]"
Output: false
Example 5:
Input: "{[]}"
Output: true
这道题让我们验证输入的字符串是否为括号字符串,包括大括号,中括号和小括号。这里需要用一个栈,开始遍历输入字符串,如果当前字符为左半边括号时,则将其压入栈中,如果遇到右半边括号时,若此时栈为空,则直接返回 false,如不为空,则取出栈顶元素,若为对应的左半边括号,则继续循环,反之返回 false,代码如下:
class Solution {
public:
bool isValid(string s) {
stack<char> parentheses;
for (int i = ; i < s.size(); ++i) {
if (s[i] == '(' || s[i] == '[' || s[i] == '{') parentheses.push(s[i]);
else {
if (parentheses.empty()) return false;
if (s[i] == ')' && parentheses.top() != '(') return false;
if (s[i] == ']' && parentheses.top() != '[') return false;
if (s[i] == '}' && parentheses.top() != '{') return false;
parentheses.pop();
}
}
return parentheses.empty();
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/20
类似题目:
Different Ways to Add Parentheses
Check If Word Is Valid After Substitutions
参考资料:
https://leetcode.com/problems/valid-parentheses/
https://leetcode.com/problems/valid-parentheses/discuss/9178/Short-java-solution
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Valid Parentheses 验证括号的更多相关文章
- [LeetCode] 20. Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [LintCode] 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 ...
- LeetCode Valid Parentheses 有效括号
class Solution { public: void push(char c){ //插入结点 struct node *n=new struct node; n->nex=; n-> ...
- [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] Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [LeetCode] Valid Palindrome 验证回文字符串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- LeetCode: Valid Parentheses 解题报告
Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', det ...
随机推荐
- C#索引器
索引器允许类或者结构的实例按照与数组相同的方式进行索引取值,索引器与属性类似,不同的是索引器的访问是带参的. 索引器和数组比较: (1)索引器的索引值(Index)类型不受限制 (2)索引器允许重载 ...
- ASP.NET + EF + SQL Server搭建个人博客系统新手系列(一):界面展示
第一次写博客,可能有些地方描述的不准确,还请大家将就.本人大四学生,学了半年C#,半年.net,但是很遗憾,学完之后只会写个hello word.老师教的过程中总是会套用一些模板,特别是后台,完全封装 ...
- [Tool] github 入手教程
简单的介绍一下 Github 的基本操作. 主页:https://github.com/ 首先自然是在 GitHub 注册一个帐号了.然后开始正文吧. Git 基本介绍 Git 是属于分布式版本控制系 ...
- 通过向页面写html代码导出excel
//excel文件名 string filename = "考勤汇总"; StringBuilder ExcelHtml = new StringBuilder(); ExcelH ...
- dpkg:处理软件包dradis (--configure)时出错
dpkg:处理软件包dradis (--configure)时出错!解决方案:1.将info文件夹更名%mv /var/lib/dpkg/info /var/lib/dpkg/info_old2.新建 ...
- Python 操作 MS Excel 文件
利用 Python 对 Excel 文件进行操作需要使用第三方库: openpyxl,可执行 pip install openpyxl 进行安装 1. 导入 openpyxl 模块 导入 openpy ...
- 9.2.2 .net framework下的MVC 控件的封装(下)
控件封装的部分说明 可能有人觉得应该前后端分离,我也承认这是应该的方向,我们也在考虑使用ng2等简化前端.但是,我们封装控件还是因为如下原因综合考虑的: 我们这是个框架,上面支撑了许多个应用,包含几百 ...
- javascript冒泡排序
数组冒泡排序算法(升序) 升序:小数在前,大数在后 冒泡排序的原则:每次比较相邻两个元素,如果前一个数>后一个数,说明违反升序的要求,就将两数交换位置.否则,保持不变.继续比较下一对. 例如:玩 ...
- 总结js常用函数和常用技巧(持续更新)
学习和工作的过程中总结的干货,包括常用函数.常用js技巧.常用正则表达式.git笔记等.为刚接触前端的童鞋们提供一个简单的查询的途径,也以此来缅怀我的前端学习之路. PS:此文档,我会持续更新. Aj ...
- AEAI ESB培训大纲
1. 概述 本文档的目的是为了让使用者能更好的操作.维护.服务于整个ESB系统平台,该信息系统平台不仅需要成熟稳定的产品,更需要技术熟练的运行维护人员,以便能更好地进行科学有效的运行维护工作. AEA ...