问题:给定一个字符串,判断每个字符串都不一样
思路:ASCII为长度为8的字符集,总共有256种可能。申明一个bool类型的数组a,先遍历一遍字符串,将每个字母对应的ASCII值作为a数组的下标,记录每个字符是否出现。若已出现返回false,否则true。
#include <iostream>
#include<assert.h>
#include<string.h>
using namespace std;
bool isUniqueChar(const char * s)
{
assert(s!=NULL);
bool *a =new bool[256];
memset(a,false,256);
while(*s!='\0')
{
if(a[int(*(s++))])
return false;
else
a[int(*(s++))]=true;
}
if(*s=='\0')
return true;
return false;
}
int main()
{
char a[100]="asdffghkljl";
char b[100]="asdgh";
cout<<isUniqueChar(a)<<endl;
cout<<isUniqueChar(b)<<endl;
return 0;
}
输出结果
0
1