题解 | #找出字符串中第一个只出现一次的字符#
找出字符串中第一个只出现一次的字符
http://www.nowcoder.com/practice/e896d0f82f1246a3aa7b232ce38029d4
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
char[] chrs = scan.nextLine().toCharArray();
Queue<Character> queue = new LinkedList<>();
int[] nums = new int[26];
for (char chr : chrs) {
nums[chr - 'a']++;
if (nums[chr - 'a'] == 1) {
queue.add(chr);
}
if (nums[chr - 'a'] > 1 && queue.contains(chr)) {
queue.remove(chr);
}
}
if (queue.isEmpty()) {
System.out.println(-1);
} else {
char chr = queue.poll();
System.out.println(chr);
}
}
}