题解 | #找出字符串中第一个只出现一次的字符#
找出字符串中第一个只出现一次的字符
https://www.nowcoder.com/practice/e896d0f82f1246a3aa7b232ce38029d4
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
//使用哈希表存储各个字符出现的次数
//再次遍历数组,利用数组顺序,获得第一个只出现1次的字符
int main() {
string str;
cin >> str;
unordered_map<char, int> mp;
for(char ch:str){
mp[ch]++;
}
for(char ch:str){
if(mp[ch]==1){
cout << ch;
return 0;
}
}
cout << -1;
return 0;
}
// 64 位输出请用 printf("%lld")
查看12道真题和解析
