题解 | #找出字符串中第一个只出现一次的字符#
找出字符串中第一个只出现一次的字符
https://www.nowcoder.com/practice/e896d0f82f1246a3aa7b232ce38029d4
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); scanner.useDelimiter("\n"); while (scanner.hasNext()) { String S = scanner.next(); int[] or = new int[128]; Arrays.fill(or, S.length()); for (int i = 0; i < S.length(); i++) { if (or[S.charAt(i)]!=S.length()) { or[S.charAt(i)]=S.length()+1; continue; } or[S.charAt(i)]=i; } OptionalInt min = Arrays.stream(or).min(); System.out.println(min.getAsInt() < S.length() ? S.charAt(min.getAsInt()) : "-1"); } } }