题解 | #找出字符串中第一个只出现一次的字符#
找出字符串中第一个只出现一次的字符
https://www.nowcoder.com/practice/e896d0f82f1246a3aa7b232ce38029d4
#include <iostream> #include <unordered_set> #include <vector> using namespace std; int main() { string str; cin >> str; // 遍历字符串统计出现次数 vector<int> count(128, 0); for (auto& c : str) { count[c]++; } // 重新遍历字符串找到第一个出现一次的字符 for (int i = 0; i < str.size(); i++) { if (count[str[i]] == 1) { cout << str[i] << endl; break; } // 最后一次也没找到出现一次字符,输出-1 if (i == str.size() - 1) { cout << -1 << endl; } } return 0; } // 64 位输出请用 printf("%lld")