华为OJ 初级:去除重复字符并排序

去除重复字符并排序:

输入:              字符串
输出:              去除重复字符并排序的字符串
样例输入:       aabcdefff
样例输出:       abcdef

统计出现的过的字符串,并将相应的数组位置为1,然后遍历数组,为1的将对应的字符输出。

package com.huawei;

import java.util.Scanner;

public class Norepeat {
	
	public static String noRepeat(String str){
		
		char[] chars = new char[255];
		int temp;
		
		StringBuffer sb = new StringBuffer();
		
		for (int i = 0; i < str.length(); i++) {			
			temp = str.charAt(i);
			if(chars[temp] == 0){
				chars[temp] = 1;
			}			
		}
		
		for (int i = 0; i < chars.length; i++) {
			if(chars[i] == 1){
				sb.append((char)i);
			}
		}
		return sb.toString();
	}

	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		System.out.println("输入任意字符串:");
		String input = sc.nextLine();
		sc.close();		
		System.out.println(noRepeat(input));
	}

}

测试结果:

猜你喜欢

转载自blog.csdn.net/qq_32639315/article/details/81414252