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:删除最外层的括号


下一篇:1021:打印字符-信息学一本通(c++)