leetcode20 C++ 4ms 有效的括号 微软面试题

class Solution {
public:
    bool isValid(string s) {
        vector<char> stack;
        map<char, char> bra;
        bra[')'] = '(';
        bra['}'] = '{';
        bra[']'] = '[';


        for(int i=0;i<s.size();i++){
            if(s[i]=='{' || s[i]=='[' || s[i]=='('){
                stack.push_back(s[i]);
            }
            else{
                if(stack.empty() || bra[s[i]] != stack[stack.size()-1]){
                    return false;
                }
                stack.pop_back();
            }

        }

        return stack.empty();

    }
};
posted @ 2018-07-26 20:07  一条图图犬  阅读(319)  评论(0编辑  收藏  举报