第一个只出现一次的字符

来源:互联网 发布:c语言else是什么意思 编辑:程序博客网 时间:2024/06/05 21:58

代码:

#include <iostream>using namespace std;char FindFirstNotRepeatingChar(char* pString){    if (pString == nullptr)        return '\0';    const int size = 256;   //char 是8个bit的类型,总共有256个字符    int hashtable[256];    for (int i = 0; i < size; i++)    {        hashtable[i] = 0;    }    char* pHashKey = pString;    while (*pHashKey != '\0')   //第一次遍历    {        hashtable[*(pHashKey++)]++;    }    pHashKey = pString;    while (*pHashKey != '\0')               //第二次遍历    {        if (hashtable[*pHashKey] == 1)            return *pHashKey;        pHashKey++;    }    return '\0';}int main(){    char* pString = "abaccdeff";    cout << "第一个只出现一次的字符: "<<FindFirstNotRepeatingChar(pString)<<endl;    cout << endl;    system("pause");    return 0;}

测试:

这里写图片描述

0 0
原创粉丝点击