题解 | #找出字符串中第一个只出现一次的字符#
找出字符串中第一个只出现一次的字符
https://www.nowcoder.com/practice/e896d0f82f1246a3aa7b232ce38029d4
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 String s = in.nextLine(); char c; for (int i = 0; i < s.length(); i++) { c = s.charAt(i); if (isafter(c, i, s) || isbefore(c, i, s)) { continue; } else{ System.out.println(c); return; } } System.out.println("-1"); } public static boolean isafter(char c, int i, String s) { for (int j = i + 1; j < s.length(); j++) { if (c == s.charAt(j)) { return true; } } return false; } public static boolean isbefore(char c, int i, String s) { for (int k = i - 1; k >= 0; k--) { if (c == s.charAt(k)) { return true; } } return false; } }