LeetCode 1021:Remove Outermost Parentheses

Posted strugglerisnd

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 1021:Remove Outermost Parentheses相关的知识,希望对你有一定的参考价值。

C语言

技术图片
char * removeOuterParentheses(char * S){
    int len = strlen(S);
    int j = 0;
    int sum = 0;
    for(int i = 0; i < len; i++)
    {
        if (S[i] == ‘(‘)
        {
            sum += 1;
        }
        else if (S[i] == ‘)‘)
        {
            sum -= 1;
        }
        if (S[i] == ‘(‘ && sum > 1)
        {
            S[j] = ‘(‘;
            j++;
        }
        else if (S[i] == ‘)‘ && sum > 0)
        {
            S[j] = ‘)‘;
            j++;
        }
        else if (S[i] != ‘(‘ && S[i] != ‘)‘)
        {
            S[j] = S[i];
            j++;
        }
    }
    S[j] = ‘\0‘;
    return S;
}
技术图片

以上是关于LeetCode 1021:Remove Outermost Parentheses的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 1021 Remove Outermost Parentheses

LeetCode #1021. Remove Outermost Parentheses 删除最外层的括号

LeetCode --- 1021. Remove Outermost Parentheses 解题报告

LeetCode --- 1021. Remove Outermost Parentheses 解题报告

LeetCode --- 1021. Remove Outermost Parentheses 解题报告

LeetCode 1021:Remove Outermost Parentheses