【100题】找出一个字符串中第一个只出现一次的字符

来源:互联网 发布:苹果5s怎么改4g网络 编辑:程序博客网 时间:2024/05/22 18:58
#include <iostream>using namespace std;char lmf(char *pString){if(!pString){return 0;}//定义并初始化hash表unsigned int hash[256] = {0};char *pHashKey = pString;//根据字符串,计数!!!while(*pHashKey != '\0'){hash[*pHashKey]++;pHashKey++;}//复原pHashKey = pString;//再次遍历字符串,取得第一个只出现一次的字符while(*pHashKey != '\0'){if(hash[*pHashKey] == 1){return *pHashKey;}pHashKey++;}return 0;}void main(){char *a = "abbaccdeeffggh";char ch = lmf(a);putchar(ch);}