代码拉取完成,页面将自动刷新
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "./uthash/include/uthash.h"
struct HashTable
{
int key, val;
UT_hash_handle hh;
};
int maxSubAryLen(int* nums, int numSize, int k)
{
int maxLen = 0;
struct HashTable* cnt = NULL;
int preSum = 0;
for (int i = 0; i < numSize; ++i)
{
preSum += nums[i];
struct HashTable* tmp = NULL;
int need = preSum - k;
HASH_FIND_INT(cnt, &need, tmp);
if (preSum == k) {
maxLen = i + 1;
} else if (tmp != NULL) {
maxLen = fmax(maxLen, i - tmp->val);
printf("\t%d, %d, %d, %d\n", i, maxLen, need, tmp->val);
}
HASH_FIND_INT(cnt, &preSum, tmp);
if (tmp == NULL) {
struct HashTable* newSum = (struct HashTable*)malloc(sizeof(struct HashTable));
newSum->key = preSum, newSum->val = i;
printf("%d, %d, %d\n", i, newSum->key, newSum->val);
HASH_ADD_INT(cnt, key, newSum);
}
}
return maxLen;
}
int main()
{
int nums[] = {-2, -1, 2, 1};
int k = 1;
int res = maxSubAryLen(nums, 4, k);
printf("***%d", res);
return 0;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。