题解 | #找出字符串中第一个只出现一次的字符#
找出字符串中第一个只出现一次的字符
https://www.nowcoder.com/practice/e896d0f82f1246a3aa7b232ce38029d4
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
int main() {
unordered_map<char, int> count;
string str;
string min;
char ch = '\0';
cin >> str;
for (auto x : str)
{
count[x]++;
}
for (auto p : count)
{
if (p.second == 1)
{
min += p.first;
}
}
if (min.size() == 0)
{
cout << -1;
}
else
{
for (auto x : str)
{
for (auto c : min)
{
if (x == c)
{
ch = x;
break;
}
}
if (ch != '\0')
{
cout << ch;
break;
}
}
}
return 0;
}
巨人网络成长空间 50人发布
查看1道真题和解析