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);
}
}
這道題的考察點在于需要處理有相鄰重複字元的處理方式和重複字元在字元串末尾的字元操作,準确判定就沒有問題啦!!