题解 | #找出字符串中第一个只出现一次的字符# 计数数组统计出现次数
找出字符串中第一个只出现一次的字符
http://www.nowcoder.com/practice/e896d0f82f1246a3aa7b232ce38029d4
注意题目并没有限制字符串中所有字符均为字母,所以要考虑到所有字符的情况。
#include<bits/stdc++.h>
using namespace std;
const int N =1010;
int cnt[N];
int main()
{
string s;
for (; cin >> s; memset(cnt, 0, sizeof(cnt))) {
for (const auto &c : s)
cnt[c]++;
bool is_found = false;
for (const auto &c : s) {
if (cnt[c] == 1) {
is_found = true;
cout << c << endl;
break;
}
}
if (!is_found) cout << -1 << endl;
}
return 0;
} 