Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.
Examples:
s = "leetcode" return 0. s = "loveleetcode", return 2.
分析:
使用查找表
代码:
class Solution {
public:
int firstUniqChar(string s) {
int slength;
int stdtable[1000];
memset(stdtable,0,sizeof(stdtable));
slength=s.length();
for(int i=0; i<slength; i++)
{
stdtable[s[i]-' ']++;
}
for(int i=0; i<slength; i++)
{
if(stdtable[s[i]-' ']==1)
{
return i;
}
}
return -1;
}
};