现在的位置: 首页 > 综合 > 正文

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

2013年08月09日 ⁄ 综合 ⁄ 共 396字 ⁄ 字号 评论关闭
#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);
}

抱歉!评论已关闭.