题解 | #找出字符串中第一个只出现一次的字符#
找出字符串中第一个只出现一次的字符
https://www.nowcoder.com/practice/e896d0f82f1246a3aa7b232ce38029d4
import java.util.Scanner; // 使用indexOf和lastIndexOf快捷判断 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextLine()) { String s = in.nextLine(); int length = s.length(); String ans = "-1"; for (int i = 0; i < length; i++) { char c = s.charAt(i); if (s.indexOf(c) == s.lastIndexOf(c)) { ans = String.valueOf(c); break; } } System.out.println(ans); } } }