题目描述
在一个字符串(0<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要区分大小写).
class Solution {
public:
int FirstNotRepeatingChar(string str) {
vector<char> one;
vector<char> two;
int size = str.size();
for(int i = 0; i < size; i++)
{
if(one.empty())
{
one.push_back(str[i]);
two.push_back(str[i]);
}
else
{
int j = 0;
for(j = 0; j < one.size(); j++)
{
if(one[j] == str[i])
break;
}
if(j == one.size())
{
two.push_back(str[i]);
one.push_back(str[i]);
}
else
{
one.push_back(str[i]);
for(int k = 0; k < two.size(); k++)
{
if(str[i] == two[k])
{
two.erase(two.begin()+k);
break;
}
}
}
}
}
for(int i = 0; i < str.size(); i++)
{
if(str[i] == two[0])
return i;
}
return -1;
}
};