题目来源:牛客网
题目描述
在一个字符串(1<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置
题目解析;
用map记录每个字符的次数,返回次数为1的的位置。
代码:
class Solution {
public:
int FirstNotRepeatingChar(string str) {
map<char,int>map;
for(auto it = str.begin();it!=str.end();map[*it++]++);
for(auto it = str.begin();it !=str.end();it++)
{
if(map[*it] == 1)
return it - str.begin();
}
return -1;
}
};