模板
while (right < numsSize) { //窗口右边界
right++
while(左邊界移动条件){
left++
}
长度最小的子数组
int minSubArrayLen(int target, int* nums, int numsSize){
if (numsSize == 0) {
return 0;
}
int sum = 0;
int ans = INT_MAX;
int left = 0;
int right = 0;
while (right < numsSize) { //窗口右边界
sum += nums[right];//右边界zhong'h
while (sum >= target) {
ans = fmin(ans, right - left + 1);
sum -= nums[left];
left++;
}
right++;
}
return ans == INT_MAX ? 0 : ans;
}
找到字符串中所有字母异位词
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
#define MAX_LENGTH 123
#define MAX_RET 30001
int* findAnagrams(char * s, char * p, int* returnSize){
int l = 0;
int r = 0;
int shash[MAX_LENGTH] = {0};
int phash[MAX_LENGTH] = {0};
int slen = strlen(s);
int plen = strlen(p);
*returnSize = 0;
if(slen < plen) {
return NULL;
}
int *ret = calloc(MAX_RET, sizeof(int));
for (int i = 0; i < plen; i++) {
phash[p[i]]++;
}
int diffNum = 0;
//printf("%d", 'z');
for (int i = 0; i < MAX_LENGTH; i++) {
if((phash[i]) > 0) {
diffNum++;
};
}
int vaild = 0;
while (r < slen) {
char sr = s[r];
r++;
if (phash[sr] > 0){
shash[sr]++;
if (shash[sr] == phash[sr]) {
vaild++;
}
}
while (r - l >= plen) {
if(vaild == diffNum) {
ret[(*returnSize)++] = l;
}
char sl = s[l];
l++;
if (phash[sl] > 0){
if (shash[sl] == phash[sl]) {
vaild--;
}
shash[sl]--;
}
}
}
return ret;
}
字符串的排列
#define MAX_NUM 123
#define TRUE 1
#define FALSE 0
bool checkInclusion(char * s1, char * s2){
int stringLen = strlen(s2);
int subStringLen = strlen(s1);
int stringHash[MAX_NUM] = {0};
int substringHash[MAX_NUM] = {0};
int right = 0;
int left = 0;
int diffNum = 0;
int vaild = 0;
for(int i = 0; i < subStringLen; i++) {
substringHash[s1[i]]++;
}
for(int i = 0; i < MAX_NUM; i++) {
if(substringHash[i] > 0) {
diffNum++;
}
}
while (right < stringLen) {
char rightc = s2[right];
right++;
if(substringHash[rightc] > 0) {
stringHash[rightc]++;
if(stringHash[rightc] == substringHash[rightc]) {
vaild++;
printf("%d %d",stringHash[rightc] ,substringHash[rightc]);
}
}
while (right - left >= subStringLen) {
if(vaild == diffNum) {
return TRUE;
}
char leftc = s2[left];
left++;
if(substringHash[leftc] > 0) {
if(stringHash[leftc] == substringHash[leftc]) {
vaild--;
}
stringHash[leftc]--;
}
}
}
return FALSE;
}
最大连续1的个数 II
#define MAX_LEN 2
int findMaxConsecutiveOnes(int* nums, int numsSize){
int right = 0;
int left = 0;
int numHash[MAX_LEN] = {0};
int maxlen = 0;
int maxn = 0;
while (right < numsSize) {
if (nums[right] == 1) {
numHash[nums[right]]++;
}
maxn = fmax(maxn, numHash[nums[right]]);
right++;
while(right - left > 1 + maxn) {
numHash[nums[left]]--;
left++;
}
maxlen = fmax(maxlen, right - left);
}
return maxlen;
}
爱生气的书店老板
先把所有不生气的求一个和,再加上滑动窗口中最大的窗口
#define MAX_LEN 20001
int maxSatisfied(int* customers, int customersSize, int* grumpy, int grumpySize, int minutes){
int sum = 0;
int increase = 0;
int hash[MAX_LEN] = {0};
for (int i = 0; i < customersSize; i++) {
if (grumpy[i] == 0) {
sum = sum + customers[i];
hash[i + 1] = hash[i];
} else {
hash[i + 1] = hash[i] + customers[i];
}
}
for (int i = minutes; i < customersSize + 1; i++) {
increase = fmax(increase, hash[i] - hash[i - minutes]);
}
sum = sum + increase;
return sum;
}