天天看点

给定一个长度为字符串, 需要去除所有之前曾经出现过的字符,只保留第一次出现的字符

public class Derived{
    public String unique_string (String s) {
        // write code here
        String aa = new String();
        char[] chars = s.toCharArray();
        int j ;
        Boolean flag;
        for(int i = 0; i < chars.length; i++){
            flag = true;
          for(j = 0;j < aa.length(); j++){
              if(chars[i] == aa.charAt(j)){
                  flag = false;
                  break;
              }
          }
          if(flag){
              aa += chars[i];
          }
        }
        return aa;
    }

    public static void main(String[] args) {
        Derived derived = new Derived();
      	Scanner input = new Scanner(System.in);
        System.out.print("请输入需要去重的字符:");
        String str = input.next();
        String qwyqcbhaqijv = derived.unique_string(str);
        System.err.println(qwyqcbhaqijv);
    }
}
           

这道题的考察点在于需要处理有相邻重复字符的处理方式和重复字符在字符串末尾的字符操作,准确判定就没有问题啦!!