码迷,mamicode.com
首页 > 其他好文 > 详细

【哈希表】

时间:2014-09-21 19:55:31      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   ar   for   div   sp   on   log   

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;
}

 

【哈希表】

标签:style   blog   color   ar   for   div   sp   on   log   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!