30,403
社区成员




1.首先发现大错
没看懂题目,直接就上了。
但我给想成那啥一定要按顺序排列了,然后。。。。
2.修改问题
比如字符串1"abc",那么字符串2中包含有abc随机组成的字串都是可以的比如,abc,acb,bac,bca,cab,cba这些都是可以算作组成。只要字符串2中包含有这6中组成中的一种即可返回true
源代码如下
bool checkInclusion(char* s1, char* s2)
{
int n = strlen(s1), m = strlen(s2);//n,m分别对应1,2的长度
if (n > m)
{//如果字符串1大于字符串2的长度直接返回false
return false;
}
int cnt[26];//建立小写字母数组
memset(cnt, 0, sizeof(cnt));//为每个字母对应的数组空位置0
for (int i = 0; i < n; ++i)
{
--cnt[s1[i] - 'a'];//将字符串1内的字符进行统计(按负数统计)
}
int left = 0;
for (int right = 0; right < m; ++right)
{
int x = s2[right] - 'a';//获取right对应字母在cnt中的位置
++cnt[x];//计数加一
while (cnt[x] > 0)
{ //当所计数的字母数在字符串2内的数目大于字符串1内的数目
--cnt[s2[left] - 'a']; //left对应字母在cnt中计数减1
++left; //left右移
}
if (right - left + 1 == n)
{ //若左右指针间长度等于s1的长度
return true; //返回true
}
}
return false;
}