去除重复字符并排序:
输入: 字符串
输出: 去除重复字符并排序的字符串
样例输入: aabcdefff
样例输出: abcdef
统计出现的过的字符串,并将相应的数组位置为1,然后遍历数组,为1的将对应的字符输出。
package com.huawei;
import java.util.Scanner;
/**
* Created by A5313 on 2015/8/10.
*/
public class Norepeat {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入任意字符串:");
String input = sc.nextLine();
System.out.println(noRepeat(input));
}
public static String noRepeat(String str){
char[] chars = new char[255];
char[] input = str.toCharArray();
int temp;
for(int i = 0;i< input.length;i++){
temp = input[i];
if(chars[temp] == 0)
chars[temp] = 1;
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < chars.length; i++) {
if(chars[i] == 1)
sb.append((char)i);
}
return sb.toString();
}
}

本文介绍如何使用Java解决字符串中去除重复字符并进行排序的问题。样例输入为'aabcdefff',经过处理后,输出的字符串为'abcdef'。方法是统计每个字符出现的情况,将出现过的字符存入数组并标记,最后遍历数组,输出未重复的排序后字符。

3313

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



