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. 【BZOJ4028】[HEOI2015]公约数数列 分块

    [BZOJ4028][HEOI2015]公约数数列 Description 设计一个数据结构. 给定一个正整数数列 a_0, a_1, ..., a_{n - 1},你需要支持以下两种操作: 1. M ...

  2. Spring MVC中的模型数据处理

    一.综述 Spring MVC 提供了以下途径来输出模型数据: 1.ModelAndView 当处理方法返回值类型为 ModelAndView时, 方法体即可通过该对象添加模型数据到请求域. 2.Ma ...

  3. iOS使用正则匹配限制输入密码格式

    1.代码实现"密码至少为9位,并需包含大写字母.小写字母.数字或特殊字符等三种" 返回0.1.2为格式不正确,返回4为密码格式正确 -(int)checkIsHaveNumAndL ...

  4. 我的Android进阶之旅------>如何获取Android控件的宽和高

    本文转载于:http://blog.csdn.net/johnny901114/article/details/7839512 我们都知道在onCreate()里面获取控件的高度是0,这是为什么呢?我 ...

  5. spring属性注入方式

    一.使用有参构造注入属性 配置文件 constructor-arg标签是需注入属性的名字 User类 生成了User的有参构造函数 测试类 结果 打印出了name属性的值 二.使用set方法注入属性 ...

  6. 关于Ninja中上传下载文件

    上传得时候 根据类型来判断一下: 然后下载的时候需要:在url的最后一个/ 后面加上你要下载的类型如/download/app.apk; 下载成功就是app.apk了;

  7. C#练习委托、事件、事件处理

    控制台应用程序效果: 代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; ...

  8. easy_install和pip的安装及使用

    在终端输入命令报错后,在网上找到了这篇博客,用easy_install命令安装pip,问题解决 Fatal error in launcher: Unable to create process us ...

  9. parent

    <?php class MyObject { function myMethod() { //标准功能 echo "Standard Functionality\n"; } ...

  10. Android SDK上手指南1:应用程序结构

    一直说要学java要学android开发,可是一直胡乱地忙活这忙活那,之前开始学了一点也中断了.说是没时间,都是借口,回顾一下自己的生活感觉缺少点激情,没有什么奋斗的动力,所以好多时间就浪费了.刚刚考 ...