您的位置:首页 > 其它

20.Valid Parentheses

2015-06-06 11:57 316 查看
思路:普通的栈

class Solution {

public:

    bool isValid(string s) {

        stack<char> the_stack;

        for(int i=0;i<s.size();++i){

            if(s[i]=='('|| s[i]=='{'|| s[i]=='['){

                the_stack.push(s[i]);

            }else if(!the_stack.empty()){

                char pre=the_stack.top();

                if( (s[i]==')' && pre=='(') || (s[i]==']' && pre=='[') || (s[i]=='}' && pre=='{') ){

                    the_stack.pop();

                }else{

                    return false;

                }

            }else{

                return false;

            }

        }

        if(the_stack.empty())

            return true;

        else

            return false;

    }

    

};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: