class Solution {
public:
string removeDuplicates(string s) {
stack<char> st;
int len=s.size();
for(int i=0;i<len;i++){
if(!st.empty()){
if(s[i]!=st.top()){
st.push(s[i]);
}else{
st.pop();
}
}else{
st.push(s[i]);
}
}
int lens=st.size();
s.resize(lens);
int i=0;
while(!st.empty()){
char scur=st.top();
st.pop();
s[i++]=scur;
}
reverse(s.begin(),s.end());
return s;
}
};
leetcode 1047.删除字符串中的所有相邻重复项
最新推荐文章于 2025-11-24 01:23:29 发布
371

被折叠的 条评论
为什么被折叠?



