678. 有效的括号字符串
2020-07-03 本文已影响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;
}
};