题解 | #点击消除#
点击消除
https://www.nowcoder.com/practice/8d3643ec29654cf8908b5cf3a0479fd5
#include <iostream> using namespace std; //1.给两个string ,ret为最终结果; //2.用string模拟 ret栈的结构,最后直接输出就行,不用逆置了 //3.遍历当str,与ret的前一个比较,相同就不尾插 int main() { string str,ret; cin >> str; ret += str[0]; for (size_t i = 1;i < str.size(); i++) { if(ret[ret.size() - 1] != str[i]) ret += str[i]; else //如果相等 ret.pop_back(); } if(ret.size() != 0) cout << ret << endl; else cout << '0' << endl; return 0; }