class Solution {
public String removeDuplicates(String S) {
if(S.length()==1) return S;
char c;
Stack<Character> stack=new Stack<>();
StringBuffer list=new StringBuffer();
stack.push(S.charAt(0));
for(int i=1;i<S.length();i++){
c=S.charAt(i);
if(stack.empty()) stack.push(c);
else {
if(c==stack.peek()) stack.pop();
else stack.push(c);
}
}
while(!stack.empty()){
list.append(stack.pop());
}
return list.reverse().toString();
}
}
删除字符串中的所有相邻重复项
最新推荐文章于 2023-06-12 17:13:06 发布