题目:在字符串中找出第一个只出现一次的字符 如输入“abaccdeff" 则输出‘b'
从前向后遍历每一个字符 然后将当前字符与后面的每个字符比较 如果发现没有重复的字符则该字符就只出现一次 时间复杂度为O(n*n)
我们可以统计每个字符出现的次数 需要一个数据容器来统计每个字符的出现次数
#include <iostream>
#include <string>
using namespace std;
int main()
{
char *s=new char [100];//
int *num = new int[256];
memset(num, 0, sizeof(int)*256);
cin.getline(s,100);
if (s == NULL)
return -1;
for (int i = 0;i<strlen(s);i++)
{
num[int(s[i])]= num[int(s[i])] +1;
}
for (int i = 0;i < strlen(s);i++)
{
if (num[int(s[i])] == 1)
{
cout << s[i] << endl;
return 0;
}
}
cout << "Not Have!" << endl;
delete[] num;
delete[]s;
return 0;
}