题解 | #找出字符串中第一个只出现一次的字符#
找出字符串中第一个只出现一次的字符
https://www.nowcoder.com/practice/e896d0f82f1246a3aa7b232ce38029d4
//时间复杂度O(n^2),可以利用数组下标统计出现次数,时间复杂度能降到O(2n)
#include <stdio.h>
#include<string.h>
int main()
{
char arr[1000];
gets(arr);
int sz=strlen(arr),i,j,count=0,flag=1;
for(i=0;i<sz;i++)
{
count=0;
for(j=0;j<sz;j++)
{
if(arr[j]==arr[i])
{
count++;
}
}
if(count==1)
{
printf("%c\n",arr[i]);
flag=0;
return 0;
}
}
if(flag)
{
printf("-1\n");
}
return 0;
}
查看16道真题和解析