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. IntelliJ IDEA集成svn

    IntelliJ IDEA如何集成svn呢? 1. 首先配置下载并配置svn软件,推荐使用SlikSvn. 下载地址https://sliksvn.com/download/,下载最近版本

  2. newClass a = Func(3)中隐藏的操作

    缘起 #include <iostream> #include <bitset> using namespace std; class A { public: A() { co ...

  3. apache缓存

    http://www.t086.com/article/4256 http://www.360doc.com/content/09/0928/13/41237_6551659.shtml

  4. tcpdump抓SQL

    前言:假设如果有个服务器几十个链接突然达到上千个链接,show processlist,general_log,还有慢查询日志这些都不能用,你怎么把这些链接过来的SQL情况了解清楚,如果你觉得那些好用 ...

  5. awk输出单引号,双引号

    双引号: awk '{print "\""}'        #放大:awk '{print "  \"  "}' 使用“”双引号把一个双引 ...

  6. [HDOJ1827]Summer Holiday(强连通分量,缩点)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1827 缩点后统计入度和当前强连通分量中最小花费,然后记录入度为0的点的个数和花费和就行了. /* ━━ ...

  7. servlet应用具体实例

    web,xml应用文件 1.<filter>参数 <filter> <filter-name>encodingFilter</filter-name> ...

  8. Codeforces Round #248 (Div. 2) C. Ryouko's Memory Note (vector 替换)

    题目链接 题意:给m个数字, 这些数字都不大于 n,  sum的值为相邻两个数字 差的绝对值.求这n个数字里把一个数字 用 其中另一个数字代替以后, 最小的sum值. 分析:刚开始以为两个for 最坏 ...

  9. HDU 1397 Goldbach's Conjecture【素数打表】

    题意:给出n,问满足a+b=n且a,b都为素数的有多少对 将素数打表,再枚举 #include<iostream> #include<cstdio> #include<c ...

  10. poj 1364 King(差分约束)

    题意(真坑):傻国王只会求和,以及比较大小.阴谋家们想推翻他,于是想坑他,上交了一串长度为n的序列a[1],a[2]...a[n],国王作出m条形如(a[si]+a[si+1]+...+a[si+ni ...