美文网首页
valid-parentheses

valid-parentheses

作者: DaiMorph | 来源:发表于2019-08-21 17:13 被阅读0次
class Solution {
public:
    bool isValid(string s) {
        string left="{[(",right="}])";
        stack<char>st;
        for(auto c:s)
        {
            if(left.find(c)!=string::npos)
                st.push(c);
            else{
                if(st.empty()||st.top()!=left[right.find(c)])
                    return false;
                st.pop();
            }
        }
        return st.empty();
    }
};

相关文章

网友评论

      本文标题:valid-parentheses

      本文链接:https://www.haomeiwen.com/subject/wfyisctx.html