在一个字符串中找到第一个只出现一次的字符,要求时间复杂度O(n)

来源:互联网 发布:临沂知豆电动汽车 租赁 编辑:程序博客网 时间:2024/05/17 01:11
// 在一个字符串中找到第一个只出现一次的字符.cpp : Defines the entry point for the console application.////利用hash表,记录每一个字符出现的次数//char占8个位,字符的最大可能数是2^8=256#include "stdafx.h"#include <iostream>using namespace std;const int TableSize=256;char FindFirstNoRepeateChar(char *str){unsigned int hashTable[TableSize];for(int i=0;i<TableSize;i++)hashTable[i]=0;char *strTmp=str;while((*strTmp)!='\0')hashTable[(*strTmp++)]++;strTmp=str;while((*strTmp)!='\0'){if(hashTable[(*strTmp++)]==1)return *(--strTmp);}return *strTmp;}int _tmain(int argc, _TCHAR* argv[]){char str[]="ab$d+a+cb$fde";cout<<str<<endl;cout<<FindFirstNoRepeateChar(str)<<endl;system("pause");return 0;}

原创粉丝点击