LeetCode 1021.删除最外层的括号
遇到左括号 +1
遇到右括号 -1
当值为0时为完整括号
类似于栈顶指针操作
class Solution {
public:
string removeOuterParentheses(string S) {
string ret;
for(int i = 0, pre = 0, cnt = 0; i < S.size(); i++){
if(S[i] == '(') cnt += 1;
else cnt -= 1;
if(cnt != 0) continue;
ret += S.substr(pre + 1, i - pre - 1);
pre = i + 1;
}
return ret;
}
};