public class Solution { public int FirstNotRepeatingChar(String str) { // 使用两个长度为26的数组,统计每个字符出现的次数,然后再按顺序来一次,找到第一个只出现一次的。 int[] number = new int[26]; int[] lNumber = new int[26]; int res = -1; for (int i = 0; i < str.length(); ++i) { int index = str.charAt(i) - 'a'; if (index < 0) { index = str.charAt(i) - 'A'; lNumber[index] += 1; } else { number[index] += 1; } } for (int i = 0; i < str.length(); ++i) { int index = str.charAt(i) - 'a'; if (index < 0) { index = str.charAt(i) - 'A'; if (lNumber[index] == 1) { res = i; break; } } else if (number[index] == 1) { res = i; break; } } return res; } }