查找字符串中出现一次且第一次出现的字符
2016-08-09 20:45
225 查看
原理:利用哈希表的算法进行查找,创建一个能存储256个数的数组,第一次对出现的字符进行统计,第二次查找出现一次的字符并返回.
#include<stdio.h>
#include<stdlib.h>
char FirstNotRepeatingChar(char *pString)
{
if (pString == NULL)
{
return '\0';
}
const int tableSize = 256;
unsigned int hashTable[tableSize];
for (unsigned i = 0; i < tableSize; 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 *str = "abcde.fghabcdefdsdsfdsada";
char c = FirstNotRepeatingChar(str);
printf("%c", c);
system("pause");
return 0;
}
运行结果:
#include<stdio.h>
#include<stdlib.h>
char FirstNotRepeatingChar(char *pString)
{
if (pString == NULL)
{
return '\0';
}
const int tableSize = 256;
unsigned int hashTable[tableSize];
for (unsigned i = 0; i < tableSize; 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 *str = "abcde.fghabcdefdsdsfdsada";
char c = FirstNotRepeatingChar(str);
printf("%c", c);
system("pause");
return 0;
}
运行结果:
相关文章推荐
- 剑指offer——查找一个字符串中第一次出现一次的字符(哈希直接定址法)
- [字符串]在一个字符串中查找第一次只出现一次的字符
- 从字符串中查找第一次出现一次的字符
- 字符串查找:(第一次)&&(只出现一次的字符)
- 从面试题中学算法(1)--哈希表查找字符串中第一次仅出现一次的字母
- 统计一个字符串中第一次只出现一次的字符
- O(n)时间效率寻找字符串中第一次出现一次的字符
- 剑指offer-第五章优化时间和空间效率(在字符串中第一次出现切只出现一次的字符)
- 查找字符串中第一个只出现一次的字符
- 在字符串中查找第一个只出现一次的字符
- 华为OJ:查找字符的第一个字符串只出现一次
- 【C语言】模拟实现strchr函数.即在一个字符串中查找一个字符第一次出现的位置并返回
- 字符串-查找第一个只出现一次的字符
- 字符串中第一次出现的只出现一次的字符
- 查找字符串中第一个出现一次的字符
- 找出字符串中第一次出现一次的字符
- Google笔试题:哈希表的一个意想不到的应用--在字符串中查找第一个只出现一次的字符
- c语言,strchr(),查找字符串中第一次字符出现的位置
- 查找字符串中只最早的出现一次的字符
- 【c语言】模拟实现strchr函数,功能:在一个字符串中查找一个字符第一次出现的位置,如果没有出现返回NULL