在一个字符串中找到第一个只出现三次的字符(如accbcabbbdddbb中第一个只出现三次的字符是c)

//没有找到返回-1,找到返回找到的字符
char FindFirst(char* str)
{
#define MAX_CHAR_NUM 128
	int Harsh[MAX_CHAR_NUM] = {0};
	char FisrtAppearPos[MAX_CHAR_NUM];
	memset(FisrtAppearPos,-1,sizeof(FisrtAppearPos));

	for (char* p=str; *p!=0; p++)
	{
		if (++Harsh[*p] == 3)
			FisrtAppearPos[*p] = p-str;
		else
			FisrtAppearPos[*p] = -1;
	}
	int nPos = MAX_CHAR_NUM;
	for(int i=0; iFisrtAppearPos[i])
				nPos = FisrtAppearPos[i];
	}
	if (nPos == MAX_CHAR_NUM)//没有找到
		return -1;
	return str[nPos];
}

你可能感兴趣的:(每日一练)