leetcode-algorithms-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

解法

通过栈实现匹配.

class Solution
{
public:
bool isValid(string s)
{
std::stack<char> matchs;
for (int i = 0; i < s.size(); ++i)
{
switch(s[i])
{
case '(':
case '{':
case '[':
matchs.push(s[i]);
break;
case ')':
if (matchs.size() <= 0 || matchs.top() != '(') return false;
matchs.pop();
break;
case '}':
if (matchs.size() <= 0 || matchs.top() != '{') return false;
matchs.pop();
break;
case ']':
if (matchs.size() <= 0 || matchs.top() != '[') return false;
matchs.pop();
break;
}
}
return matchs.empty();
}
};

链接: leetcode-algorithms 目录

leetcode-algorithms-20 Valid Parentheses的更多相关文章

  1. [Leetcode][Python]20: Valid Parentheses

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 20: Valid Parentheseshttps://oj.leetcod ...

  2. 《LeetBook》leetcode题解(20):Valid Parentheses[E]——栈解决括号匹配问题

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  3. C# 写 LeetCode easy #20 Valid Parentheses

    20.Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...

  4. 【一天一道LeetCode】#20. Valid Parentheses

    一天一道LeetCode系列 (一)题目 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...

  5. LeetCode题解(20)--Valid Parentheses

    https://leetcode.com/problems/valid-parentheses/ 原题: Given a string containing just the characters ' ...

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

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

  7. LeetCode:20. Valid Parentheses(Easy)

    1. 原题链接 https://leetcode.com/problems/valid-parentheses/description/ 2. 题目要求 给定一个字符串s,s只包含'(', ')',  ...

  8. 【LeetCode】20. Valid Parentheses

    题目:

  9. LeetCode解题笔记 - 20. Valid Parentheses

    这星期听别人说在做LeetCode,让他分享一题来看看.试了感觉挺有意思,可以培养自己的思路,还能方便的查看优秀的解决方案.准备自己也开始. 解决方案通常有多种多样,我觉得把自己的解决思路记录下来,阶 ...

  10. 20. Valid Parentheses【leetcode】

    20. Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...

随机推荐

  1. jvm:垃圾收集器

    垃圾收集器: Serial 收集器: 单线程收集器,专注做收集,会暂停别的工作.收集效果好. ParNew 收集器: 是Serial的多线程版本.目前只有它能和CMS收集器配合.    Paralle ...

  2. Linux 解决 firefox 中文页面乱码问题

    1.由于 firefox 默认是允许网页自己选择字体,在 Linux 上便会出现部分网站的乱码情况.因此可以取消允许页面自己选择字体这个选项便能解决部分乱码情况.

  3. js实现类型jq的dom加载完成

    有时候我们只想在 dom 加载完成后运行 js ,而不是等所有图片加载完成.所以不需要 onload , onload 会加载图片等其他媒体.很消耗时间. 原:http://blog.csdn.net ...

  4. 20. --erg--=--org--=--urg-- 做,工作 (词20、21)

    词汇速记21

  5. Spring项目JUnit测试报错ClassNotFoundException解决

    Eclipse项目上有红色感叹号,各包显示正常.用JUnit测试部分能运行,部分报错,报错如下: Class not found UserTestjava.lang.ClassNotFoundExce ...

  6. 在用node安装某个全局模块的时候,没有权限修改node_modules

    一.问题 今天在安装公司内部的一个npm模块的时候,发现报错了⬇ 第一行错误: deprecated graceful-fs@1.2.3: please upgrade to graceful-fs ...

  7. IPC 之 Socket 的使用

    一.概述 我们知道在开发中,即时通讯.设备间的通信都是使用 Socket 实现,那当然用它来实现进程间通信更是不成问题.Socket 即套接字,是一个对 TCP / IP协议进行封装 的编程调用接口( ...

  8. 关于Java类和包的那些事

    *.Java文件 问题:一个.java源文件中是否可以包括多个类(不是内部类)?有什么限制? 答案:可以有多个类,但只能有一个public的类,并且public的类名必须与文件名相一致.一个文件中可以 ...

  9. [Android] for ArcFace Demo

    虹软人脸识别引擎Android的Demo演示,可以直接下载使用 下载地址 https://github.com/asdfqwrasdf/ArcFaceDemo 工程如何使用? 1.下载代码: git ...

  10. 学习笔记40—endnote点点滴滴

    1.在endnote中,如果引用书籍时,需要作者名称后面加“逗号”,作者名称才不会缩写, 比如: 1) 加“逗号”之前: 2)加“逗号”之后