解题思路:
(1)使用unordered_map存储<char,pair<int,int>>
(2)即对应字符出现的次数和一开始出现的位置
class Solution {
public:
int FirstNotRepeatingChar(string str) {
unordered_map<char,pair<int,int>> mp;
for(int i=0;i<str.length();i++) {
if(mp.find(str[i])==mp.end()) mp[str[i]].second=i;
mp[str[i]].first++;
}
int min = INT_MAX;
for(auto it=mp.begin();it!=mp.end();it++) {
if(it->second.first==1&&it->second.second<min) min = it->second.second;
}
return mp.size()==0?-1:min;
}
};