华为OJ题目(一):找出字符串中第一个出现一次的字符

来源:互联网 发布:icp备案后怎么使用域名 编辑:程序博客网 时间:2024/05/19 06:19
 #include <string> #include <iostream> using namespace std; char Findchar(char* pInputString){ if (!pInputString)   return 0;   //输入不合法int hashTable[256] = { 0 };     //创建一个哈希表(即创建一个数组),并初始化//确定字符串中每个字符出现的次数char* pHashKey = pInputString;   //pInputString指针的首地址赋给pHashKey,*pHashKey对应字符串的值while (*pHashKey != '\0'){hashTable[*(pHashKey)]++;pHashKey++;}//找到字符串中只出现一次的那个字符 pHashKey = pInputString;  //指针再次指向首地址,再遍历一次 while (*pHashKey != '\0'){if (hashTable[*pHashKey] == 1)return *pHashKey;pHashKey++;} return 0;  //如果这个字符串为空,或者字符串中的每个字符都至少出现两次} int main() { char str[1000]; gets(str); char temp = Findchar(str); if (temp == 0) cout << "." << endl; else cout << temp << endl; return 0; }

1 0