题解 | #找出字符串中第一个只出现一次的字符#
找出字符串中第一个只出现一次的字符
https://www.nowcoder.com/practice/e896d0f82f1246a3aa7b232ce38029d4
#include <iostream> #include <map> #include <unordered_map> using namespace std; int main() { string s; cin >> s; unordered_map<char, int> mp; for(int i = 0; i < s.length(); i++) { int t = 1; if(mp.count(s[i])) { t = mp.find(s[i])->second + 1; } // mp.insert(pair<char, int>(s[i], t)); mp[s[i]] = t; } for(int i = 0; i < s.length(); i++) { if(mp.count(s[i]) && mp.find(s[i]) -> second == 1) { cout << s[i] << endl; return 0; } } cout << -1 << endl; return 0; } // 64 位输出请用 printf("%lld")
本可以用int直接模拟的,没咋做过map,想联系一下C++map,结果发现一个知识点。
insert不会覆盖,
下标插入才会覆盖。