美文网首页
678. 有效的括号字符串

678. 有效的括号字符串

作者: 来到了没有知识的荒原 | 来源:发表于2020-07-03 23:10 被阅读0次

678. 有效的括号字符串

[low,high]表示当前左括号的数量范围
如果最后low>0 则说明左括号没匹配完,如果high<0说明左括号不够用了

class Solution {
public:
    bool checkValidString(string s) {
        int low=0,high=0;
        for(auto c:s){
            if(c=='(')low++,high++;
            else if(c=='*')low=max(0,low-1),high++;
            else if(c==')'){
                low=max(0,low-1);
                high--;
                if(high<0)return false;
            }
        }
        return low==0;
    }
};

相关文章

网友评论

      本文标题:678. 有效的括号字符串

      本文链接:https://www.haomeiwen.com/subject/ghaeqktx.html