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.

题解:

  括号匹配问题,与出现顺序有关,联想到栈和队列。

Solution 1

 class Solution {
public:
bool isValid(string s) {
if (s.empty())
return true;
stack<char> st; for (int i = ; i < s.size(); ++i) {
char cur = s[i];
if (cur == '[' || cur == '(' || cur == '{') {
st.push(cur);
continue;
}
if (st.empty())
return false;
if (cur == ']' && st.top() != '['
|| cur == '}' && st.top() != '{'
|| cur == ')' && st.top() != '(')
return false;
st.pop();
} return st.empty();
}
};

【LeetCode】020. Valid Parentheses的更多相关文章

  1. 【leetcode】Longest Valid Parentheses

    Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...

  2. 【leetcode】 Longest Valid Parentheses (hard)★

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  3. 【LeetCode】20. Valid Parentheses 有效的括号

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:有效,括号,括号匹配,栈,题解,leetcode, 力扣 ...

  4. 【LeetCode】20. Valid Parentheses

    题目:

  5. 【LeetCode】36. Valid Sudoku 解题报告(Python)

    [LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...

  6. 【LeetCode】593. Valid Square 解题报告(Python)

    [LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  7. 【LeetCode】678. Valid Parenthesis String 解题报告(Python)

    [LeetCode]678. Valid Parenthesis String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...

  8. 【LeetCode】65. Valid Number

    Difficulty: Hard  More:[目录]LeetCode Java实现 Description Validate if a given string can be interpreted ...

  9. 【LeetCode】680. Valid Palindrome II

    Difficulty:easy  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/valid-palindrome ...

随机推荐

  1. smarty模板 变量 运算符 表达式 流程控制 函数

    ① 从配置文件中读取配置: 1,在模板页面加载配置文件 html页面 不是php页面<{config_load file='fo.conf'}> 2,在需要用到配置的地方加<{#si ...

  2. PHP、jQuery、AJAX和MySQL 数据库实例

    index.html页面 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...

  3. Netty聊天室(2):从0开始实战100w级流量应用

    目录 客户端 Client 登录和响应处理 写在前面 客户端的会话管理 客户端的逻辑构成 连接服务器与Session 的创建 Session和 channel 相互绑定 AttributeMap接口的 ...

  4. vue表单输入的绑定

    vue的核心:声明式的指令和数据的双向绑定. 那么声明式的指令,已经给大家介绍完了.接下来我们来研究一下什么是数据的双向绑定? 另外,大家一定要知道vue的设计模式:MVVM M是Model的简写,V ...

  5. 蜗牛—ORACLE基础之触发器学习(三)

    版权声明:本文为大腰子原创文章,如若转载,请标明原地址. https://blog.csdn.net/u010071361/article/details/30037215 建立一个触发器, 当职工表 ...

  6. ABAP 面向对象(Object Orientation) OO

    [转自 http://blog.sina.com.cn/s/blog_7c7b16000101bhof.html]在程序中, 对象的识别和寻址是通过对象引用来实现的, 对象引用变量可以访问对象的属性和 ...

  7. SpringBoot学习笔记(4):添加自定义的过滤器

    SpringBoot:学习笔记(4)——添加自定义的过滤器 引入自定义过滤器 SpringBoot提供的前端控制器无法满足我们产品的需求时,我们需要添加自定义的过滤器. SpringBoot添加过滤器 ...

  8. python打包工具 --- pyinstaller

    安装 安装python并添加到环境变量之后,在终端执行如下命令即可: pip install pyinstaller 截图如下: 若安装失败,可到: https://www.lfd.uci.edu/~ ...

  9. ELK日志分析系统-Logstack

    ELK日志分析系统 作者:Danbo 2016-*-* 本文是学习笔记,参考ELK Stack中文指南,链接:https://www.gitbook.com/book/chenryn/kibana-g ...

  10. maven setting.xml 中文配置详解(全配置)

    春节假期在家养病,乘有时间整理了下之前的知识——知识贵在归纳总结. 参照了官方文档,针对其中的一些未描述详尽的内容翻查了不少资料,补充到了配置文件中,同时再加上一些说明.例子,方便查阅. 内容虽然比较 ...