1 Star 0 Fork 1

陈鹏/leecode with labuladong

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
325.和等于K的最长子数组长度.c 1.16 KB
一键复制 编辑 原始数据 按行查看 历史
陈鹏 提交于 2022-08-14 22:06 . c day2
#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;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/Chan1998/leecode-with-labuladong.git
git@gitee.com:Chan1998/leecode-with-labuladong.git
Chan1998
leecode-with-labuladong
leecode with labuladong
master

搜索帮助