leetcode+ 栈的经典题

点击打开链接
class Solution {
public:
    bool isValid(string s) {
        stack<char> st;
        if(s.size()==0) return true;
        for(int i=0;i<s.size(); i++){
            if(s[i]=='{' || s[i]=='[' || s[i]=='(') st.push(s[i]);
            else if(s[i]=='}'){
                if(!st.empty()&&st.top()=='{') st.pop();
                else return false;
            }
            else if(s[i]==']'){
                if(!st.empty()&&st.top()=='[') st.pop();
                else return false;
            }
            else if(s[i]==')'){
                if(!st.empty()&&st.top()=='(') st.pop();
                else return false;
            }
        }
        if(!st.empty()) return false;
        else return true;
    }
};



版权声明:本文为u013554860原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。