题目描述

在一个字符串(0<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要区分大小写).

解决方案

class Solution {
public:
    int FirstNotRepeatingChar(string str) {
        int hashtable[256] = {0};
        for(auto e:str)
            hashtable[e]++;
        for(auto e:str)
            if(hashtable[e] == 1){
                return (int)str.find(e);
            }
        return -1;
    }
};