题目
算法思想:用一个变量记录左括号数,匹配+1,不匹配减一,然后根据是否是外层括号把字符加入到结果中。
代码:
string removeOuterParentheses(string S) {
string result = "";
int num = 0;//左括号数量
for(int i = 0;i < S.length();i++)
{
if(S[i] == '(')
{
num++;
if(num > 1)
result += S[i];
}
else
{
num--;
if(num >= 1)
result += S[i];
}
}
return result;
}