#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
题目:在一个字符串中找到第一个只出现一次的字符。如输入abaccdeff,则输出b
解法:考虑用一个hash表记录每个字符出现的次数,然后顺序遍历字符串,同时判断hash值是否为1
*/
int main()
{
char str[99]="abaccdeff";
int strLength=strlen(str);
int hashTable[256]={0};
for(int i=0;i<strLength;++i){
hashTable[str[i]]++;
}
for(int j=0;j<strLength;++j){
if(hashTable[str[j]]==1){
printf("%c",str[j]);
break;
}
}
return 0;
}
在一个字符串中找到第一个只出现一次的字符。如输入abaccdeff,则输出b
最新推荐文章于 2021-08-14 20:46:41 发布