题目描述:在一个字符串(0<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要区分大小写).
用一个哈希表来记录每个字符出现的次数,然后返回第一个出现次数为1的字符。
class Solution {
public:
int FirstNotRepeatingChar(string str)
{
int hashtable[256] = { 0 };
for (int i = 0; i < str.size(); i++)
{
hashtable[str[i]]++;
}
for (int i = 0; i < str.size(); i++)
{
if (hashtable[str[i]] == 1)
{
return i;
}
}
return -1;
}
};