题解 | 找出字符串中第一个只出现一次的字符
找出字符串中第一个只出现一次的字符
https://www.nowcoder.com/practice/e896d0f82f1246a3aa7b232ce38029d4
#include <bits/stdc++.h>
using namespace std;
int main()
{
string str;
cin>>str;
int len = str.size();
for(int i=0;i<len;i++)
{
int cnt = 0;
for(int j=0;j<len;j++)
{
if(str[i] == str[j])
{
cnt++;
if(cnt > 1) break;
}
}
if(cnt == 1)
{
cout<<str[i];
return 0;
}
}
cout<<-1<<endl;
return 0;
}
查看3道真题和解析