首页 > 学院 > 开发设计 > 正文

在一个字符串中找到第一个只出现一次的字符。如输入abaccdeff,则输出b

2019-11-06 08:34:06
字体:
来源:转载
供稿:网友
#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;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表