题解 | #找出字符串中第一个只出现一次的字符#
找出字符串中第一个只出现一次的字符
https://www.nowcoder.com/practice/e896d0f82f1246a3aa7b232ce38029d4
#include <algorithm>
#include <bits/stdc++.h>
#include <climits>
using namespace std;
int main() {
string s;
cin >> s;
unordered_map<char, int> map;
for(int i = 0; i < s.size(); ++i){
auto it = map.find(s[i]);
if(it == map.end()){
map[s[i]] = 1;
}else{
map[s[i]]++;
}
}
vector<char> res;
for(auto &i : map){
if(i.second == 1){
res.push_back(i.first);
}
}
if(res.size() == 0){
cout << "-1" << endl;
}else if(res.size() == 1){
cout << res[0] << endl;
}else{
int index = INT_MAX;
for(int i = 0; i < res.size(); ++i){
int it = s.find(res[i]);
index = min(index, it);
}
cout << s[index] << endl;
}
}
// 64 位输出请用 printf("%lld")
上海得物信息集团有限公司公司福利 1237人发布