首页 > 其他分享 >LeetCode20. Valid Parentheses

LeetCode20. Valid Parentheses

时间:2022-08-15 23:24:32浏览次数:37  
标签:Parentheses return int else Valid push false st LeetCode20

题意

  • 序列含有'{}', '()', '[]', 判断其是否有效

方法

  • stack

代码


bool isValid(string s) {
    int N = s.size();
    if (N & 1) return false;
    stack<char> st;
    for (int i = 0; i < N; i++) {
        if (s[i] == '(') st.push(')');
        else if (s[i] == '{') st.push('}');
        else if (s[i] == '[') st.push(']');
        else if (st.empty() || st.top() != s[i]) return false;
        else st.pop();
    }

    return st.empty();
}

标签:Parentheses,return,int,else,Valid,push,false,st,LeetCode20
From: https://www.cnblogs.com/Figure_at_a_Window/p/16590040.html

相关文章