字符串中第一个出现2次的字符

上午做的去哪网的笔试题,由于心态问题,没发挥好,其中第二题是写一个函数,能够返回字符串中第一个出现2次的字符,当时写的是两次遍历字符串的方法,下来回想一下,用一个哈希表就可以了,代码如下:

void main()
{
 char str[]="fabcdefga";
 const int num = 256;
 int count[num] = {0};
 for(int i = 0; i < strlen(str); i ++ )
 {
  count[str[i]]++;
 }
 for(int j = 0; j < strlen(str); j ++ )
 {
  if(count[str[j]]==2)
   printf("%c",str[j]);
  break;
 }
}

猜你喜欢

转载自blog.csdn.net/songzhaorong/article/details/39185251