天天看点

LeetCode_20_valid parentheses

class Solution {
public:
    bool isValid(string s) {
        stack<char> st;
        map<char,char> dict;
        dict['}']='{';
        dict[']']='[';
        dict[')']='(';
        for(int i=0;i<s.size();i++){
            if(dict.find(s[i])!=dict.end()){
                char temp;
                if(!st.empty()){//在对stack进行读操作前,必须要判断stack是否为空
                    temp=st.top();
                    st.pop();
                }
                if(temp!=dict[s[i]])
                    return false;
            }
            else
                st.push(s[i]);
        }
        if(st.empty())
            return true;
        else
            return false;
    }
};