100题_13 第一个只出现一次的字符

题目:在一个字符串中找到第一个只出现一次的字符。如输入abaccdeff,则输出b。


这是一道超级大水题,注意英文字符的话,最多只有256个,这样咱就可以很简单地统计每个字符出现的次数,其实对于这题来讲的话,咱其实都不用去统计出现的次数,每个字符用两个位来记录其状态就可以了。但是为了程序的简单,我们这里使用int来计数。

 

代码:

View Code
#include <iostream>

using namespace std;

char findFirstOne(char* s) // 查找第一个值出现一次的字符
{
    
int count[256];
    
for (int i = 0; i < 256; i++)
        count[i] 
= 0;
    
char *= s;
    
while (*c)
        count[
*(c++)] ++;
    
for (int i = 0; i < 256; i++)
        
if (count[i] == 1)
            
return i;
    
return 0;
}


int main()
{
    
char *= "abaccdeff";
    cout
<<findFirstOne(s)<<endl;
    
return 0;
}

 

posted on 2011-03-07 13:34  小橋流水  阅读(141)  评论(0编辑  收藏  举报

导航