题解 | #有效括号序列#

有效括号序列

https://www.nowcoder.com/practice/37548e94a270412c8b9fb85643c8ccc2

#include <stack>

class Solution {
public:
    bool isValid(string s) {
        stack<char> validator;
        for (const char& cur: s)
        {
            if (left.find(cur) != left.end())
                validator.push(cur);
            else
            {
                if (validator.empty())
                    return false;
                if (right.at(validator.top()) != cur)
                    return false;
                else
                    validator.pop();
            }
        }
        if (validator.empty())
            return true;
        else
            return false;
    }
private:
    const unordered_set<char> left {'(','[','{'};
    const unordered_map<char, char> right {{'(', ')'},{'[', ']'},{'{', '}'}};
};

全部评论

相关推荐

07-23 12:04
门头沟学院 Java
现在是很缺人吗
码农索隆:缺分母,不缺分子,这样好作为炫耀的资本
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务