[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 ...
 
随机推荐
- 【Linux命令】Linux命令后面所接选项和参数的区别
			
Linux命令后面所接选项和参数的区别 在使用Linux命令时,有时候后面会跟一些"选项"(options)或"参数"(agruments) 命令格式为: #中 ...
 - redis之通信协议
			
Redis 协议将传输的结构数据分为 5 种最小单元类型,单元结束时统一加上回车换行符号\r\n. 1.单行字符串 以 + 符号开头. 2.多行字符串 以 $ 符号开头,后跟字符串长度. 3.整数值 ...
 - Vue.js安装及环境搭建
			
Vue.js环境搭建-Windows版 步骤一:安装node.js 在搭建vue的开发环境之前,需要先下载node.js,vue的运行是要依赖于node的npm的管理工具来实现,node可以在官网或者 ...
 - C# 中如何深度复制某一个类型(备注:可能有 N 个类型需要复制)的对象?
			
如题,针对某一个特定的类型,深度复制,没有那么难,最起码可以手动赋值,但如果要针对 N 多类型深度复制,最简单的方法,是把这个对象序列化成 XML.JSON 或其它可以序列化的载体,然后再将这个载体反 ...
 - webpack中使用DefinePlugin来传递构建的环境变量给源代码使用
			
最近在思考如何提供一种前后端开发功能测试既高效又安全的方案,因为对于我平时的项目是前后端同时进行的,后端我已经有了完备的权限管理,前端不能的角色会有不同的访问数据权限.而在vue前后端分离开发情况下, ...
 - 反射与类对象获取-Java学习
			
类对象 类对象指的是一个类在jvm中加载后所形成的对象,每一个类都只有一个类对象,该类对象被所有的实例对象所共享. 类之间有不同的方法,不同的属性.类对象,就是用于描述这种类,都有什么属性,什么方法的 ...
 - python 排序和查找算法
			
一.搜索 1.顺序查找 数据存储在具有线性或顺序关系的结构中时,可顺序访问查找 def sequential_search(ilist, item): pos = 0 while pos < l ...
 - element-ui的表单验证this.$refs[formName].validate的代码不执行
			
经过排查,如果自定义验证中,每种情况都要写明确和有回调函数callback var validatePhone = (rule, value, callback) => { const reg ...
 - Prometheus  node_exporter  grafana部署安装
			
1.环境 centos7 prometheus-2.10.0.linux-amd64.tar.gz node_exporter-0.18.1.linux-amd64.tar.gz 2.安装 创建sys ...
 - 11、shell_sed
			
正则表达式:正则表达式,就是用一种模式,去匹配一类字符串的公式. 正则表达式的解释是用正则表达式引擎来实现的,常用正则表达式引擎有两类: 基本正则.扩展正则. 正则表达式基础: 正则表达式是由一些 ...