个人注释
法一:
char * removeOuterParentheses(char * s) {
int len = strlen(s);
char *res = (char *)malloc(sizeof(char) * len);
char *stack = (char *)malloc(sizeof(char) * (len / 2));
int pos = 0, top = 0;
for (int i = 0; i < len; i++) {
char c = s[i];
if (c == ')') { //进stack栈
top--;
}
if (top > 0) { //如果stack栈不为空,及此时c为 原语字符串中一个字符 将其存入字符串res
res[pos++] = c;
}
if (c == '(') { //出stack栈
stack[top++] = c;
}
}
free(stack);
res[pos] = '\0'; //结束res字符串
return res;
}
法二:
作者:力扣官方题解
链接:https://leetcode.cn/problems/remove-outermost-parentheses/solutions/1520365/shan-chu-zui-wai-ceng-de-gua-hao-by-leet-sux0/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。