您的位置:首页 > 理论基础 > 数据结构算法

我思故我在系列—数据结构面试题NO.17(题目搜集整理者JULY,非常感谢!!)

2011-10-21 16:14 423 查看
经常在实际中摸爬滚打的成熟人士都很清楚,

那就是成功不会那么容易,失败是不可避免的。

因此他们决不会耗神费力让偶尔的失败腐蚀自己的坚毅,而是会用更多的力量去争取成功!!



——摘自 奥格.曼狄诺《羊皮卷》

第17题:

题目:在一个字符串中找到第一个只出现一次的字符。


如输入abaccdeff,则输出b。

哈希表的运用

#include <iostream>

#include <assert.h>

#include <string.h>

using namespace std;

char FirstNotRepeatingChar(char* pString)

{

assert(pString!=NULL);

const int tableSize = 256;/*create hashtable,key:s char, value:times*/

unsigned int hashTable[tableSize];

char* pHashKey = pString;

for(unsigned int i = 0; i < tableSize; ++ i)/*init hashtable*/

{

hashTable[i] = 0;

}

while(*(pHashKey) != '\0')/*compute times*/

{

hashTable[*(pHashKey++)] ++;

}

pHashKey = pString;

while(*pHashKey != '\0')

{

if(hashTable[*pHashKey] == 1)

{

return *pHashKey;

}

pHashKey++;

}

return *pHashKey;

}

int main()

{

cout<<"请输入一串字符:"<<endl;

char s[100];

cin>>s;

char* ps=s;

cout<<FirstNotRepeatingChar(ps)<<endl;

return 0;

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐