[LeetCode] 20. 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] 20. Valid Parentheses 验证括号的更多相关文章
- [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 (括号匹配问题)
题目链接 https://leetcode.com/problems/valid-parentheses/?tab=Description Problem: 括号匹配问题. 使用栈,先进后出! ...
- leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、
20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...
- [LeetCode] Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- leetCode 20.Valid Parentheses (有效的括号) 解题思路和方法
Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', de ...
- [LintCode] 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 input ...
- [LeetCode]20. Valid Parentheses有效的括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
随机推荐
- MySQL锁表查询SQL
// 查看进程 SHOW PROCESSLIST; // 查看是否锁表 SHOW OPEN TABLES WHERE In_use > 0; // 查看正在锁的事务 SELECT * FROM ...
- mysql-5.7.23-winx64 解压版详细安装教程
1.下载解压版: 2.配置环境变量 (1)新建MYSQL_HOME变量,并配置值为: C:\softwaretool\mysql-5.7.23-winx64 计算机→属性→高级系统设置→高级→环境变量 ...
- Nuget包管理工具(程序包控制台执行语句)
NUGET命令 注:使用前确保nuget是最新版本,升级到最新版本有两种方式: (1).CMD将nuget升级到最新版本:nuget update -self (2).扩展中查看nuget是否需要更新 ...
- Nginx反向代理YUM请求
一.安装配置Nginx服务(Nginx服务器上建议先关闭iptables/firewalld服务,待实验完成后再根据实际情况配置) [root@localhost ~]# yum install ng ...
- LocalDB 从2017更换到2014后一直显示连接不正确解决方案
问题描述:LocalDB 版本混装后出现默认实例创建不成功 无法连接到 (LocalDB)\MSSQLLocalDB. ------------------------------其他信息: 在与 S ...
- 打开centos7图形化窗口
1. yum groupinstall "X Window System" 2. export DISPLAY=172.16.4.240:0.0 3. yum -y install ...
- Python基础17
写出来的代码,若有部分不想运行,可注释掉. 看跑出来的结果,再加进来调试.
- 解决关于 npm build --prod ,出现 ERROR in budgets, maximum exceeded for initial. Budget 5 MB was exceeded by 750 kB的问题
问题: 执行命令 :npm build --pord,出现以下错误: WARNING :. Ignoring. WARNING MB was exceeded by 3.73 MB. ERROR MB ...
- 为什么我推荐你用 Ubuntu 开发?
微信.QQ.TIM.企业微信.钉钉等 1.首先需要安装 wine 环境,这里使用到开源的 deepin-wine-ubuntu (项目地址: https://github.com/wszqkzqk/d ...
- 010.[转] maven的三大生命周期
一.Maven的生命周期 Maven的生命周期就是对所有的构建过程进行抽象和统一.包含了项目的清理.初始化.编译.测试.打包.集成测试.验证.部署和站点生成等几乎所有的构建步骤. Maven的生命周期 ...