【LeetCode热题100道】20. 有效的括号

霍华德

关注

阅读 50

2022-03-24

在这里插入图片描述

class Solution {
public:
    bool isValid(string s) {

        stack<char> store;
        for(int i = 0; i < s.size(); i++)
        {
            if(s[i] == '(')
            {
                store.push(')');
            }
            else if(s[i] == '[')
            {
                store.push(']');
            }
            else if(s[i] == '{')
            {
                store.push('}');
            }
            else if(store.empty() || store.top() != s[i])
            {
                return false;
            }
            else
            {
                store.pop();
            }
        }
        return store.empty() ? true:false;
    }
};

精彩评论(0)

0 0 举报