题解 | #字符串的相邻字符去重#
字符串的相邻字符去重
https://www.nowcoder.com/practice/8c7d55263c624faa8e48a16d92c2e90d
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @return string字符串 */ string removeDuplicates(string s) { // write code here stack<char> charstack; for(int i=0; i<s.size(); i++){ if(charstack.empty()) { charstack.push(s[i]); }else if(!charstack.empty()){ if(s[i] == charstack.top()){ charstack.pop(); }else{ charstack.push(s[i]); } } } string s_r; for(int i= charstack.size()-1; i>=0; i--){ cout<<s[i]<<endl; s[i] = charstack.top(); s_r.insert(0, 1, s[i]); charstack.pop(); } return s_r; } };