题解 | #点击消除#

点击消除

https://www.nowcoder.com/practice/8d3643ec29654cf8908b5cf3a0479fd5

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
    char str[300010] = "";
    scanf("%s", str);

    int i = 0;
    int top = 0;
    char *stack = (char *)malloc(sizeof(char) * strlen(str));
    for (i = 0; (top >= 0) && (i < strlen(str)); i++)
    {
        if (top == 0)
        {
            stack[top] = str[i];
            top++;
        }
        else if (top > 0)
        {
            if (stack[top - 1] == str[i])
            {
                top--;
                stack[top] = '\0';
            }
            else
            {
                stack[top] = str[i];
                top++;
            }
        }
    }
    if (strlen(stack) == 0)
    {
        printf("0\n");
    }
    else
    {
        printf("%s\n", stack);
    }

    return 0;
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务