1.问题描述

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.

2.解法分析

典型的栈用法。

class Solution {

public:

    bool isValid(string s) {

        // Start typing your C/C++ solution below

        // DO NOT write int main() function

        vector<char> myStack;

        map<char,char>dict;

        dict.insert(make_pair('(',')'));

        dict.insert(make_pair('[',']'));

        dict.insert(make_pair('{','}'));

        

        for(int i=0;i<s.length();++i)

        {

            if(s[i]=='('||s[i]=='['||s[i]=='{')myStack.push_back(s[i]);

            else

                if(s[i]==')'||s[i]==']'||s[i]=='}')

                {

                    if(myStack.empty())return false;

                    if(dict[myStack.back()]==s[i])myStack.pop_back();

                    else return false;

                }

        }

        

        if(myStack.empty())return true;

        return false;

        

    }

};

leetcode—Valid Parentheses的更多相关文章

  1. [LeetCode] Valid Parentheses 验证括号

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

  2. LeetCode: Valid Parentheses 解题报告

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

  3. [Leetcode] valid parentheses 有效括号对

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

  4. LeetCode——Valid Parentheses

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

  5. [LeetCode] Valid Parentheses

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

  6. Python3解leetcode Valid Parentheses

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

  7. leetcode Valid Parentheses python

    # 解题思路: # 创建一个字典映射关系 dicts# 使用一个栈stk 遍历字符串s 得到一个新的字符串curItem 如果lastItem在dicts中的value和它相等 不做任何操作# 如果不 ...

  8. LeetCode Valid Parentheses 有效括号

    class Solution { public: void push(char c){ //插入结点 struct node *n=new struct node; n->nex=; n-> ...

  9. Valid Parentheses [LeetCode 20]

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

随机推荐

  1. PowerDesigner生成的ORACLE 建表脚本中去掉对象的双引号,设置大、小写

    原文:PowerDesigner生成的ORACLE 建表脚本中去掉对象的双引号,设置大.小写 若要将 CDM 中将 Entity的标识符都设为指定的大小写,则可以这么设定: 打开cdm的情况下,进入T ...

  2. Java API —— 网络编程

    1.网络编程概述     1)网络编程概述     · 计算机网络         是指将地理位置不同的具有独立功能的多台计算机及其外部设备,通过通信线路连接起来,在网络操作系统,网络管理软件及网络通 ...

  3. JS代码片段:判断一个元素是否进入可视区域

    // Determine if an element is in the visible viewport function isInViewport(element) { var rect = el ...

  4. 转:java两个jre目录和三个lib目录

    lib目录下放置着jar包.程序中的import语句找的就是这些文件!例如:import javax.servlet.RequestDispatcher;    问题在于,在cmd模式下编译,系统会提 ...

  5. HTML常用标签及其全称

    <a href="#">a 超级链接(anchor)</a>     <abbr title="abbreviation的简写"& ...

  6. ExtJs自学教程(2):从DOM看EXTJS

    <二> 从DOM看EXTJS 看标题可能有人会问,为啥好好的例子不看,得从DOM看起呢?答案是目标只为了一个:自运行结果把EXTJS看得更清楚点 首先,要看得靠点工具,带点放大镜什么吧?对 ...

  7. c# webbrowser 随机点击链接

    HtmlElementCollection hec = webBrowser1.Document.All; ; i < hec.Count; i++) { if (hec[i].GetAttri ...

  8. Building Xcode iOS projects and creating *.ipa file from the command line

    For our development process of iOS applications, we are using Jenkins set up on the Mac Mini Server, ...

  9. java--关键字和保留字

    关键字:Java的关键字对java的编译器有特殊的意义,他们用来表示一种数据类型,或者表示程序的结构等. 保留字:为java预留的关键字.现在还没用到,但是在升级版本中可能作为关键字. 访问控制:pr ...

  10. bzoj2535 2109

    做过4010这题其实就水了 把图反向之后直接拓扑排序做即可,我们可以用链表来优化 每个航班的最小起飞序号就相当于在反向图中不用这个点最迟到哪 type node=record po,next:long ...