Leetcode 20. Valid Parentheses

https://leetcode.com/problems/valid-parentheses/

class Solution {
public:
    char _match(char x){
        switch(x){
            case '(': return ')';
            case '[': return ']';
            default : return '}';
        }
    }
    bool isValid(string s) {
        /*
        最后需要为空
        */
        vector<char> A;
        for(auto &x:s){
            switch(x){
                case '(':
                case '[':
                case '{':
                    A.push_back(x);
                    break;
                default:
                    if(A.empty() || _match(A.back()) != x) return false;
                    A.pop_back();
                    break;
            }
        }
        if(A.empty()) return true;
        else return false;
    }
};
posted @ 2019-05-04 09:51  benda  阅读(77)  评论(0编辑  收藏  举报