题解 | #点击消除#
点击消除
https://www.nowcoder.com/practice/8d3643ec29654cf8908b5cf3a0479fd5
#include <stdio.h> #include <string.h> //思路:与栈顶比较,元素等于栈顶则弹出,元素不等于栈顶则压栈 char stack[300000]; int top = -1; //压栈 void push(char ch) { stack[ ++ top] = ch; }//判断结果 //出栈 void pop() { if (top != -1) { top--; } } //栈顶元素 char Top() { if (top != -1) { return stack[top]; } return '\0'; } int main() { char ch[30000000];//数组 while (scanf("%s", ch) != EOF) { // 注意 while 处理多个 case int len = strlen(ch);//获取长度 // 遍历数组 for (int i = 0; i < len; i++) { if (Top() == ch[i]) { pop(); } else { push(ch[i]); } } //输出栈元素 if (top == -1) { printf("0");//输出0 } for (int i = 0; i <= top; i++) { printf("%c",stack[i]); } printf("\n"); top = -1;//清空栈元素 } return 0; }