0
点赞
收藏
分享

微信扫一扫

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

霍华德 2022-03-24 阅读 50
c++

在这里插入图片描述

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 条评论