题意:给定字符数N,找出N个字符中第一次出现的字符。
思路:遍历字符串,记录每个字符出现的次数,存放在数组中,再遍历数组,找出第一个次数为1的字符。
代码:
package FindFirstCharacter;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class FindFirstCharacter {
public static void findFirstCharacher(String string){
int []array = new int[128];
for (int i = 0; i < string.length(); i++) {
array[string.charAt(i)]++;
}
for (int i = 0; i < string.length(); i++) {
if(array[string.charAt(i)] == 1){
System.out.println(string.charAt(i));
break;
}
}
}
public static void main(String[] args) throws NumberFormatException, IOException {
// Scanner in = new Scanner(System.in);
// int N = in.nextInt();
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
Integer N = Integer.parseInt(in.readLine());
for(int i = 0 ; i< N;i++){
String s = null;
try {
s = in.readLine();
} catch (IOException e) {
e.printStackTrace();
}
findFirstCharacher(s);
}
}
}