题解 | #有效括号序列#

有效括号序列

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

#include <stack>
class Solution {
public:
    /**
     * 
     * @param s string字符串 
     * @return bool布尔型
     */
    map<char,char> match;
    bool isleft(char c){
        if(c=='('||c=='['||c=='{') return true;
        return false;
    }
    bool isMatch(char c,stack<char>& help){
        if(help.empty()||help.top()!=match[c]) return false;
        help.pop();
        return true;
    }
    bool isValid(string s) {
        // write code here
        stack<char> help;
        match[')']='(';
        match[']']='[';
        match['}']='{';
        for(int i=0;i<s.length();i++){
            if(isleft(s[i])) help.push(s[i]);
            else{
                if(!isMatch(s[i],help)){
                    return false;
                }
            }
        }
        if(!help.empty()) return false;
        return true;
    }
};

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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