#include <iostream>
using namespace std;
/*
构建哈希表
1、遍历字符串,把字符串映射到哈希表
2、改哈希表的键值是字符,值是该字符出现的次数
3、c/c++实现需要遍历两次
*/
/*
c语言没有哈希表
*/
char FirstNotRepeatingChar(char* pstring)
{
if(pstring==nullptr)
return '\0';
const int tablesize = 256;
//构建哈希表
unsigned int hashtable[tablesize];
//初始化哈希表
for(unsigned int i =0; i<tablesize; i++)
hashtable[i] = 0;
//遍历数组,将每个字符映射到哈希表中
char* ptemp = pstring;
while(*ptemp != '\0')
hashtable[*(ptemp++)] ++;
ptemp = pstring;
while(*ptemp != '\0')
{
if(hashtable[*ptemp]==1)
return *ptemp;
ptemp++;
}
return '\0';
}
int main()
{
char* a = "abdaccelklajsd";
cout<<FirstNotRepeatingChar(a)<<endl;
return 0;
}
剑指offer-35 第一个只出现一次的字符
最新推荐文章于 2022-04-24 13:54:57 发布