1047. 刪除字符串中的所有相鄰重復(fù)項(xiàng)
給出由小寫(xiě)字母組成的字符串 S,重復(fù)項(xiàng)刪除操作會(huì)選擇兩個(gè)相鄰且相同的字母,并刪除它們。
在 S 上反復(fù)執(zhí)行重復(fù)項(xiàng)刪除操作,直到無(wú)法繼續(xù)刪除。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-691060.html
在完成所有重復(fù)項(xiàng)刪除操作后返回最終的字符串。答案保證唯一。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-691060.html
class Solution {
public:
string removeDuplicates(string s) {
stack<char> tmp;
for(int i=0;i<s.size();i++){
if(!tmp.empty() && s[i]==tmp.top()){
tmp.pop();
}else{
tmp.push(s[i]);
}
}
string res;
while(!tmp.empty()){
res=tmp.top()+res;
tmp.pop();
}
return res;
}
};
到了這里,關(guān)于1047. 刪除字符串中的所有相鄰重復(fù)項(xiàng)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!