【ceetcode】12 valid parentheses

有效的括弧匹配

注意事项:

1 使用stack

遇到'(' '[' '{'就push,遇到')' ']' '}'匹配进行pop等操作

class Solution {
public:
    bool isValid(string s) {
        stack<char> mystack;
        char tmp;
        for(char c:s){
        switch(c){
            case '(':
            case '[':
            case '{':
                mystack.push(c);
                break;
            case ')':
                if(mystack.empty()||mystack.top()!='(')    
                    return false;
                else{
                    mystack.pop();
                    break;
                }
            case ']':
             if(mystack.empty()||mystack.top()!='[')    
                    return false;
                else{
                    mystack.pop();
                    break;
                }
            case '}':
             if(mystack.empty()||mystack.top()!='{')    
                    return false;
                else{
                    mystack.pop();
                    break;
                }
            default:
                break;
        }
           
        }//end for
       
        
        return mystack.empty();
           
    }
};

posted @ 2015-05-11 21:25  自信乐观  阅读(191)  评论(0编辑  收藏  举报