#include<iostream>
#include<deque>
using namespace std;
char* firstNotRepeatingChar(char *str){
//在一个字符串中找到第一个只出现一次的字符。如输入abaccdeff,则输出b。
if(!str)
return NULL;
int count[256];
char *pCur=str;
for(int i=0;i<256;i++)
count[i]=0;
while (*pCur!='\0')
{
count[*pCur]++;
pCur++;
}
pCur=str;
while (*pCur!='\0')
{
if(count[*pCur]==1)
{
cout<<*pCur;
return pCur;
}
pCur++;
}
return NULL;
}
int main(){
char *str="dfsfsfsaasdfafljlsdsefk";
char *ch=firstNotRepeatingChar(str);
return 0;
}
#include<deque>
using namespace std;
char* firstNotRepeatingChar(char *str){
//在一个字符串中找到第一个只出现一次的字符。如输入abaccdeff,则输出b。
if(!str)
return NULL;
int count[256];
char *pCur=str;
for(int i=0;i<256;i++)
count[i]=0;
while (*pCur!='\0')
{
count[*pCur]++;
pCur++;
}
pCur=str;
while (*pCur!='\0')
{
if(count[*pCur]==1)
{
cout<<*pCur;
return pCur;
}
pCur++;
}
return NULL;
}
int main(){
char *str="dfsfsfsaasdfafljlsdsefk";
char *ch=firstNotRepeatingChar(str);
return 0;
}
本文介绍了一种使用C++编程语言解决寻找字符串中首次出现的唯一字符的方法。通过遍历字符串并利用计数数组记录每个字符的出现次数,算法能够高效地找出并返回首次仅出现一次的字符。
2340

被折叠的 条评论
为什么被折叠?



