[LeetCode] Valid Parentheses

来源:互联网 发布:ubuntu安装python模块 编辑:程序博客网 时间:2024/06/13 04:26
class Solution {public:    bool isValid(string s) {        stack<char> st;        st.push('*'); //先往栈push一个'*',防止为空,便于后面操作        for(int i = 0;i < s.length();i ++){            if(s[i] == '(' || s[i] == '{' || s[i] == '[')                st.push(s[i]);            else {                char c = st.top();                if(c == '(' && s[i] == ')'|| c == '{' && s[i] == '}' || c == '[' && s[i] == ']')                    st.pop();                else                    return false;            }        }        st.pop();//pop掉'*'        if(st.empty())            return true;        else            return false;    }};

0 0