面试题35:第一个只出现一次的字符

题目描述

在字符串中找到第一个只出现一次的字符。如输入“abaccdeff”,则输出'b'。

题目分析

剑指Offer(纪念版)P186

代码实现

char FirstNotRepeatingChar(char* pString)
{
    if(pString == NULL)
        return '\0';

    const int tableSize = 256;
    unsigned int hashTable[tableSize];
    for(unsigned int 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';
}

  

posted @ 2015-10-09 16:50  枯桃  阅读(145)  评论(0编辑  收藏  举报