题解 | #找出字符串中第一个只出现一次的字符#
找出字符串中第一个只出现一次的字符
https://www.nowcoder.com/practice/e896d0f82f1246a3aa7b232ce38029d4
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) { BufferedReader r = new BufferedReader(new InputStreamReader(System.in)); String a; try { a = r.readLine(); } catch (IOException e) { throw new RuntimeException(e); } char[] chs = a.toCharArray(); int l = chs.length, i = 0, tmp = 0; int[] cnt = new int[l]; int[] word = new int[128]; while (i < l) { tmp = chs[i]; word[tmp]++; i++; } i = 0; while (i < l) { tmp = chs[i]; cnt[i] = word[tmp]; i++; } i = 0; while (i < l) { if (cnt[i] == 1) break; i++; } System.out.print(i < l ? chs[i] : "-1"); } }