public class RepeatStr {
public static void main(String[] args) {
// delRepeat("01211113477440000447756677899123");
String str = "abcaabcd";
str = delRepeat2(str);
System.out.println(str);
// delRepeat3("abcaabcd");
}
public static String delRepeat2(String str){
char[]c = str.toCharArray();
int len = c.length;
int[]flag = new int[8];
int i;
for ( i=0;i<8;i++)
flag[i] = 0;
for (i=0;i<len;i++){
int index = (int)c[i]/32;
int shift = (int)c[i]%32;
if((flag[index]&(1<<shift))!=0){
c[i] = '\0';
}
flag[index]|=(1<<shift);
}
int l = 0;
for (i=0;i<len;i++){
if (c[i]!='\0')
c[l++] = c[i];
}
return new String(c,0,l);
}
public static void delRepeat(String str){
TreeSet<String> ts = new TreeSet<String>();
for (int i=0;i<str.length();i++){
ts.add(str.charAt(i)+" ");
}
Iterator<String> it = ts.iterator();
StringBuilder sb = new StringBuilder();
while (it.hasNext()){
sb.append(it.next());
}
System.out.print(sb.toString());
}
public static void delRepeat3(String str){
char[]chars = str.toCharArray();
List<Character>list = new ArrayList<Character>();
for (int i=0;i<chars.length;i++){
if (!list.contains(chars[i])){
list.add(chars[i]);
}
}
System.out.println(list);
}
}
删除字符串中重复的字符
最新推荐文章于 2023-07-01 11:28:28 发布