[leetcode]20. Valid Parentheses

来源:互联网 发布:手机看书软件免费下载 编辑:程序博客网 时间:2024/06/05 14:26

题目链接:https://leetcode.com/problems/valid-parentheses/

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.

class Solution {public:   bool isValid(string s)    {        unordered_map<char, char> map;        map['(']=')';        map['{']='}';        map['[']=']';                stack<char> charStack;        for(int i=0;i<s.length();i++)        {            if(map.find(s[i])!=map.end())                charStack.push(s[i]);            else            {                if(!charStack.empty()&&map[charStack.top()]==s[i])                    charStack.pop();                else                    return false;            }        }        return charStack.empty();    }};


0 0
原创粉丝点击