字符串过滤-讯飞笔试

题目:

通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串过滤程序,若字符串中出现多个相同的字符,将非首次出现的字符过滤掉。 
比如字符串“abacacde”过滤结果为“abcde”。

import java.util.Scanner;public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while(in.hasNext()) {
            String str = in.nextLine();
            getResult(str);
        }
    }

    private static void getResult(String str) {
        if(str == null || str.length() <= 0)
            return;
        
        boolean[] temp = new boolean[26];
        for(int i = 0; i < str.length(); i++) {
            if(temp[str.charAt(i) - 'a'] == false) {
                System.out.print(str.charAt(i));
                temp[str.charAt(i) - 'a'] = true;
            }
        }
    }
}
原文地址:https://www.cnblogs.com/zywu/p/5872039.html