772. 只出现一次的字符
作者:互联网
#include <iostream>
#include <cstring>
using namespace std;
int cnt[26];
char str[100010];
int main()
{
cin >> str;
for (int i = 0; str[i]; i ++ ) cnt[str[i] - 'a'] ++ ;
for (int i = 0; str[i]; i ++ )
if (cnt[str[i] - 'a'] == 1)
{
cout << str[i] << endl;
return 0;
}
puts("no");
return 0;
}
第一个程序有点慢
修改后的程序是
#include <iostream>
#include <cstring>
using namespace std;
int cnt[26];
char str[100010];
int main()
{
cin >> str;
for (int i = 0,len=strlen(str); i<len; i ++ ) cnt[str[i] - 'a'] ++ ;
for (int i = 0,len=strlen(str); i<len; i ++ )
if (cnt[str[i] - 'a'] == 1)
{
cout << str[i] << endl;
return 0;
}
puts("no");
return 0;
}
标签:字符,cnt,一次,772,int,char,++,str,include 来源: https://blog.csdn.net/qq_38054511/article/details/113571951