剑指offer:186页
题目:第一个只出现一个的字符,如输入:“abaccdeff”,则输出b
#include<iostream>
#include<string>
using namespace std;
char FirstNotRepeatingChar(char* pString)
{
/*
* 在字符串中找出第一个只出现一次的字符,如输入:“abaaffll”,则输出b
* 测试用例:1) 字符串为空;
* 2)字符串中存在只出现一次的字符
* 3)字符串中不存在只出现一次的字符
*/
if (pString == NULL)
return '\0';
const int tableSize = 256;
int hashTable[tableSize] = {0};
for (int i = 0; i < strlen(pString); i++)
{
hashTable[pString[i]]++;
}
for (int j = 0; j < strlen(pString); j++)
{
if (hashTable[pString[j]] == 1)
return pString[j];
}
return '\0';
}
/* 相关题目一
* 输入两个字符串,从第一个字符串中删除在第二个字符串中出现过的所有字符
* http://blog.csdn.net/zzran/article/details/8249418 此网址分析的比较详细
*/
char* TheLeavingString(char* s1, char* s2)
{
const int tableSize = 256;
int hashTable[tableSize] = { 0 };
//int p = 0, q = 0;
char *p = s1, *q = s1;
for (int i = 0; i < strlen(s2); i++)
{
hashTable[s2[i]]++;
}
while (*p)
{
cout << *p << " *" ;
cout << hashTable[*p] << endl;
if (hashTable[*p] == 1)
{
cout << hashTable[*p] << " ***";
p++;
}
else
{
*q = *p; //************这句话到底有什么错啊,55555555,一执行到这里就出错?????
p++;
q++;
}
}
*q = '\0';
return s1;
}
void main()
{
char* pString = "qwuwertqwert";
char first = FirstNotRepeatingChar(pString);
cout << first << endl;
// TheLeavingString(s1, s2)函数测试
char* s1 = "we areABC", *s2 = "aeiou";
char* s = TheLeavingString(s1, s2);
cout << strlen(s)<<" "<< endl;
}