#字符串中第一个只出现一次的字符#言简意赅,不用注释
找出字符串中第一个只出现一次的字符
https://www.nowcoder.com/practice/e896d0f82f1246a3aa7b232ce38029d4
#include <iostream> #include <string> #include <vector> using namespace std; #include <unordered_map> int main(){ string str; cin >> str; int len = str.size(); unordered_map<char, int> hash; for(int i = 0;i < len;i++){ hash[str[i]]++; } bool flag = 0; for(int i = 0;i < len;i++){ if(hash[str[i]] == 1){ flag = 1; cout << str[i] << endl; break; } } if(flag == 0) cout << -1 << endl; return 0; }