在一个字符串中找到第一个只出现一次的字符。如输入 abaccdeff,则输出 b。
题目不难,主要是两个条件,注意只出现一次,并且要第一个这种字符。
用stl的map什么的有点不合适,简单数组map即可。
char str_first(char* str)
{
char* ptr = str;
int map[255];
memset(map, 0, sizeof(int)*255);
// First O(N), count
while(*ptr!='char str_first(char* str)
{
char* ptr = str;
int map[255];
memset(map, 0, sizeof(int)*255);
// First O(N), count
while(*ptr!='\0')
{
map[*ptr]++;
ptr++;
}
// Second O(N), find first and only one time
ptr = str;
while(*ptr!='\0')
{
if(map[*ptr]==1)
{
return *ptr;
}
ptr++;
}
return '\0';
}')
{
map[*ptr]++;
ptr++;
}
// Second O(N), find first and only one time
ptr = str;
while(*ptr!='char str_first(char* str)
{
char* ptr = str;
int map[255];
memset(map, 0, sizeof(int)*255);
// First O(N), count
while(*ptr!='\0')
{
map[*ptr]++;
ptr++;
}
// Second O(N), find first and only one time
ptr = str;
while(*ptr!='\0')
{
if(map[*ptr]==1)
{
return *ptr;
}
ptr++;
}
return '\0';
}')
{
if(map[*ptr]==1)
{
return *ptr;
}
ptr++;
}
return 'char str_first(char* str)
{
char* ptr = str;
int map[255];
memset(map, 0, sizeof(int)*255);
// First O(N), count
while(*ptr!='\0')
{
map[*ptr]++;
ptr++;
}
// Second O(N), find first and only one time
ptr = str;
while(*ptr!='\0')
{
if(map[*ptr]==1)
{
return *ptr;
}
ptr++;
}
return '\0';
}';
}