在一个字符串(1<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置
思路:
创造一个大小为256,以字符串ASCII码为键值的哈希表,统计字符出现次数
class Solution {
public:
int FirstNotRepeatingChar(string str) {
if (str.empty())
return -1;
const int tableSize = 256;
int HashTable[tableSize] = { 0 };
for (int i = 0; i < str.length(); i++)
{
HashTable[str[i]]++;
}
for (int i = 0; i < str.length(); i++)
{
if (1 == HashTable[str[i]])
return i;
}
return 0;
}
};