leetcode 1047. 删除字符串中的所有相邻重复项

    科技2022-08-25  109

    爱消除,对对碰的基本思路, 一个栈解决 class Solution { public: string removeDuplicates(string S) { if (S.empty()) { return string(""); } stack<char> stk; for (int i = 0; i < S.length(); i++) { if (!stk.empty() && S[i] == stk.top()) { stk.pop(); continue; } stk.push(S[i]); } string s(""); while (!stk.empty()) { s += stk.top(); stk.pop(); } reverse(s.begin(), s.end()); return s; } };
    Processed: 0.008, SQL: 10