思路1:数组

    public static int FirstNotRepeatingChar3(String str) {
        int len = str.length();
        if (len == 0) {
            return -1;
        }
        char[] s = str.toCharArray();
        int[] m = new int[256];
        for (int i = 0; i < len; i++) {
            m[s[i]]++;
        }
        for (int i = 0; i < len; i++) {
            if (m[s[i]] == 1) {
                return i;
            }
        }
        return -1;
    }

思路2:哈希
先在hash表中统计各字母出现次数,第二次扫描直接访问hash表获得次数

    public static int FirstNotRepeatingChar2(String str) {
        if (str.length() == 0) {
            return -1;
        }
        char[] chars = str.toCharArray();//toCharArry():把字符串转为char字符数组;
        HashMap<Character, Integer> map = new HashMap<>();

        for (char c : chars) {
            if (map.containsKey(c)) {
                map.put(c, map.get(c) + 1);
            } else {
                map.put(c, 1);
            }
        }

        for (int i = 0; i < str.length(); i++) {
            if (map.get(str.charAt(i)) == 1) {
                return i;
            }
        }
        return -1;
    }