题目:在一个字符串中找到第一个只出现一次的字符。如输入abaccdeff,则输出b。
此题目比较简单,因为字符常用的就那么几个,无论怎么样都不会超过256个,所以通过建立一个临时数组来保存是否是第一次出现。
#include <iostream>
using namespace std;
void showFirst(char* s)
{
int a[256] = {0};
int n = 0;
for(;*s != '\0';s ++)
{
if(a[(unsigned)(*s)] == 0)
{
a[(unsigned)(*s)] = 1;
}
else
{
a[(unsigned)(*s)] = -1;
}
}
for(int i = 0; i < 256; i ++)
{
if(a[i] == 1)
{
cout << (char)i << endl;
break;
}
}
}
int main(int argc, char *argv[])
{
showFirst("abacbdcdf");
return 0;
}