题解 | #找出字符串中第一个只出现一次的字符#
找出字符串中第一个只出现一次的字符
https://www.nowcoder.com/practice/e896d0f82f1246a3aa7b232ce38029d4
#include <iostream> #include <type_traits> using namespace std; #include <map> #include <string> int main() { string str; getline(cin,str); map<char,int> letters; for(char & i : str){ if(letters.count(i) == 0){ letters.insert(pair<char,int>(i,1)); } else{ letters[i]++; } } bool flag = true; int i = 0; while(flag && i < str.size()){ if(letters[str[i]] == 1){ cout << str[i]; flag = false; } i++; } if(flag){ cout << -1; } }
使用一个map存储字母出现信息
华为机试刷题记录 文章被收录于专栏
记录一下手打代码的解题思路方便复习