问题:

Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.

An input string is valid if:

  1. Open brackets must be closed by the same type of brackets.
  2. Open brackets must be closed in the correct order.

Note that an empty string is also considered valid.

C++参考代码:

class Solution {
public:
    bool isValid(string s) {
        unordered_map<char,int> m;
        vector<int> v;
        m['('] = 1;
        m[')'] = -1;
        m['{'] = 2;
        m['{'] = -2;
        m['{'] = 3;
        m['{'] = -3;
        for(int i=0;i<s.length();i++){
            if(m[s[i]]>0){
                v.push_back(m[s[i]]);
            }else if(!v.empty() &&( m[s[i]] + v[v.size()-1] )== 0){
                v.pop_back();
            }else{
                return false;
            }
        }
        if(v.empty()){
            return true;
        }else {
            return false;
        }
       
    }
};

转载于:https://www.cnblogs.com/wzy-coding/p/9676224.html

LeetCode : Valid Parentheses相关推荐

  1. leetcode—Valid Parentheses

    1.问题描述 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if t ...

  2. LeetCode --- Valid Parentheses

    题目链接 Problem discription Given a string containing just the characters '(', ')', '{', '}', '[' and ' ...

  3. LeetCode Valid Parentheses(判断括号是否匹配)

    题意:给出(,[,{,),],}括号组成的字符串, 判断括号是否匹配 思路:用栈结构判断 1.如果是(,[,{,将其入栈 2.如果遇到),],},看栈是否为空.栈顶元素是否对应为(,[,{ 3.将字符 ...

  4. Leetcode: Valid Parentheses

    与 POJ 上那道括号匹配相比, 这道可谓简单 思路: 堆栈存储符号, 遇到匹配弹出 代码: #include <iostream> #include <stack> usin ...

  5. LeetCode 之 JavaScript 解答第20题 —— 有效的括号(Valid Parentheses)

    Time:2019/4/11 Title: Valid Parentheses Difficulty: Easy Author: 小鹿 题目:Valid Parentheses Given a str ...

  6. LeetCode Longest Valid Parentheses

    原题链接在这里:https://leetcode.com/problems/longest-valid-parentheses/ 题目: Given a string containing just ...

  7. LeetCode 32. Longest Valid Parentheses

    问题链接 LeetCode 32. Longest Valid Parentheses 题目解析 给出只包含左右括号的字符串,返回最长的括号匹配字符串长度. 解题思路 括号匹配问题一般借助 栈,便于理 ...

  8. LeetCode算法入门- Longest Valid Parentheses -day12

    LeetCode算法入门- Longest Valid Parentheses -day12 Given a string containing just the characters '(' and ...

  9. LeetCode算法入门- Valid Parentheses -day11

    LeetCode算法入门- Valid Parentheses -day11 题目描述: Given a string containing just the characters '(', ')', ...

最新文章

  1. SystemTap了解
  2. SQLSever 存储过程创建
  3. linux oracle12c dbca,Linux下Oracle 12c R2图形化安装笔记
  4. throws关键字_异常处理的第一种方式,交给别人处理
  5. php 正则提取日期,PHP正则匹配日期和时间(时间戳转换)的实例代码
  6. 学python用什么软件-初学 Python 需要安装哪些软件?
  7. Fiddler 手机抓包详解
  8. 浅谈股价预测模型:全能大明星——神经网络模型
  9. 如何修改wifi密码(wifi密码怎么修改在手机上怎么修改)
  10. java poi xml转word文档_使用POI转换word doc文件
  11. 2019年阿里最新java编程规范-在线版
  12. 反恐精英online单机版有各种武器
  13. 基于Bootstrap的响应式网页
  14. 计算机发展史的十大成就,我国在自然科学领域的十大成就都有哪些?
  15. 超微服务器只显示raid0,超微主板怎么创建RAID磁盘阵列 服务器组建RAID0、RAID1图文教程...
  16. MBR20100FCT低压降肖特基二极管ASEMI原装
  17. Livezilla安装配置, APE(Ajax Push Engine)安装配置
  18. cenos7开启SMB文件共享,小米摄像头无法搜索到的原因
  19. ngx-datatable的使用
  20. pandas 的基本使用

热门文章

  1. for vue 一行2列_awk 处理文本:行转列,列转行
  2. c语言中定义结构体指针的作用,c语言定义结构体指针
  3. 机器学习(MACHINE LEARNING)交叉验证(简单交叉验证、k折交叉验证、留一法)
  4. python【蓝桥杯vip练习题库】ADV-100第二大整数
  5. python微信自动机器人
  6. php获取图高度,PHP imagesx()、imagesy() – 获取图像宽度与高度_程序员人生
  7. $dbms=mysql_MySQL学习笔记(一)
  8. dedecms 如何调用多个不同栏目的文章_SEO排名,同一篇文章,在多个栏目,如何处理?...
  9. (5.2) Tomcat 8 源码, 初始化组件
  10. java 注解生成实例_Java 8 开发的 Mybatis 注解代码生成工具