哈希表实现:
char find(char *p)
{
if(p==NULL)
{
cout<<"error"<<endl;
return -1;
}
const int size=256;
int hashTable[size]={0};
char *key=p;
while(*(key)!='\0')
{
hashTable[*key++]++;
}
key=p;
while(*key!='\0')
{
if(hashTable[*key]==1)
return *key;
key++;
}
cout<<"All char are repeat!"<<endl;
return -1;
}
int _tmain(int argc, _TCHAR* argv[])
{
char s[]="abdaccdbeef";
cout<<find(s)<<endl;
return 0;
}
char findone(string &s)
{
int len=s.size();
int i;
for(i=0;i<len;i++)
{
int beg=s.find(s[i]);
int end=s.find_last_of(s[i]);
if(beg==i&&end==i)
{
break;
}
}
if(i==len)
{
cout<<"Not found!"<<endl;
return -1;
}
else
return s[i];
}
int _tmain(int argc, _TCHAR* argv[])
{
string s("abdaccbeff");
cout<<findone(s)<<endl;
return 0;
}
posted on 2012-05-22 19:42
qiushao 阅读(231)
评论(0) 编辑 收藏 引用