Valid Parentheses

/*
判断括号匹配问题,用一个栈保存判断即可
*/
class Solution {
public:
    bool isValid(string s) {
        int len = s.length();
        stack<char>s1;
        for(int i = 0 ; i < len ; i++){
            if(s[i] == '(') s1.push(')');
            else if(s[i] == '[') s1.push(']');
            else if(s[i] == '{') s1.push('}');
            else if(!s1.empty() && s1.top() == s[i]){
                s1.pop();
            }else return false;
        }
        if(s1.empty()){
            return true;
        }
        return false;
    }
};

 

posted @ 2015-04-14 20:13  SprayT  阅读(94)  评论(0编辑  收藏  举报