您的位置:首页 > 其它

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

2013-07-01 09:54 441 查看
// 在一个字符串中找到第一个只出现一次的字符.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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐