第一个只出现一次的字符

题目描述

在一个字符串(1<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置
 
public class Solution {
    public int FirstNotRepeatingChar(String str) {
        char[] ch = str.toCharArray();
        int[] a = new int['z' + 1]; //注意一定要加一,否则数组长度是122(0~121),没有z位置了
        for (char d : ch)
            a[(int) d]++;
        for (int i = 0; i < ch.length; i++)
            if (a[(int) ch[i]]== 1)
                return i;
        return -1;
    }
}
posted @ 2018-10-11 18:45  MarkLeeBYR  阅读(99)  评论(0)    收藏  举报