例如:输入:“aabcceddabc” 输出:“dabc” 4
String s = "aabcceddabc";
Set set = new HashSet();
int i = 0;
int j = 0;
String str = "";
while (i < s.length() && j < s.length()) {
if (!set.contains(s.charAt(j))) {
set.add(s.charAt(j++));
} else {
set.remove(s.charAt(i++));
}
}
for(int k = 0;k<set.toArray().length;k++){
str += set.toArray()[k];
}
System.out.println(str);
本文介绍了一种用于去除字符串中重复字符的算法实现。通过使用HashSet数据结构来判断字符是否已存在,从而确保最终输出的字符串不包含重复的字符。该算法通过遍历输入字符串并动态调整HashSet的内容来达到目的。
364

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



