- 题目描述
2. 解题思路:
创建两个整型的哈希表,双指针控制一个里面只放p,一个里面只放长度是p长度中s,不断增加 减少去比对
bool isMath(int *a, int *b)
{for (int i = 0; i < 26; i++) {if (a[i] != b[i]) {return false;}}return true;
}
int* findAnagrams(char * s, char * p, int* returnSize){int slen = strlen(s);int plen = strlen(p);int *res = (int *)malloc(sizeof(int) * slen);int k = 0;int left = 0;int right = plen - 1;if (slen < plen) {* returnSize = k;return res;}int s1[26] = {0};int p1[26] = {0};for (int i = 0; i < plen; i++) {s1[s[i] - 'a']++;p1[p[i] - 'a']++;}while (right < slen) {if (isMath(s1, p1)) res[k++] = left;if (right + 1 < slen) {s1[s[right + 1] - 'a']++;s1[s[left] - 'a']--;}left++;right++;}* returnSize = k;return res;
}