算法学习笔记[LeetCode]Valid Parentheses

来源:互联网 发布:win8 怎么下载软件 编辑:程序博客网 时间:2024/06/06 02:51
class Solution {public:    bool isValid(string s) {        int len = s.size();        stack<char> st;        for (int i = 0; i < len; i++) {            char pin = s[i];            if (st.size() != 0) {                if (st.top() == '(' && pin == ')')                    st.pop();                else if (st.top() == '[' && pin == ']')                    st.pop();                else if (st.top() == '{' && pin == '}')                    st.pop();                else st.push(pin);            }            else st.push(pin);        }        return st.empty();    }};
这道题的Tag直接给了我答案,代码运行的过程就像一个消消乐游戏一样,一旦匹配成功就那个匹配的括号就消失
0 0
原创粉丝点击