题解 | #找出字符串中第一个只出现一次的字符#
找出字符串中第一个只出现一次的字符
https://www.nowcoder.com/practice/e896d0f82f1246a3aa7b232ce38029d4
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
String str;
while ((str=reader.readLine())!=null){
findfirstChar(str);
}
}
public static void findfirstChar(String str) {
char[] arr = str.toCharArray();
int[] count = new int[128];
for (int i = 0; i < arr.length; i++) {
count[arr[i]]++;
}
boolean ret = false;
for (int i = 0; i < arr.length; i++) {
if (count[arr[i]] == 1) {
System.out.println(arr[i]);
ret = true;
break;
}
}
if (!ret) {
System.out.println(-1);
}
}
}
查看30道真题和解析