首页 > 其他 > 详细

【哈希表】

时间:2014-09-21 19:03:22      阅读:255      评论:0      收藏:0      [点我收藏+]
char FirstNotRepeatingChar(char* pString)
{
    // invalid input
    if(!pString)
        return 0;

    // get a hash table, and initialize it
    const int tableSize = 256;                    //字符1个字节8位
    unsigned int hashTable[tableSize];          //unsigned??
    for(unsigned int i = 0; i < tableSize; ++ i)
        hashTable[i] = 0;                        //为什么要置0?

    // get the how many times each char appears in the string
    char* pHashKey = pString;
    while(*(pHashKey) != \0)
        hashTable[*(pHashKey++)] ++;

    // find the first char which appears only once in a string
    pHashKey = pString;
    while(pHashKey)
    {
        if(hashTable[*pHashKey] == 1)
            return *pHashKey;

        pHashKey++;
    }

    // if the string is empty
    // or every char in the string appears at least twice
    return 0;
}

 

【哈希表】

原文:http://www.cnblogs.com/zhangXH/p/3984827.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!