Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If there isn't one, return 0 instead.

Example 1:

Given nums = [1, -1, 5, -2, 3]k = 3,
return 4. (because the subarray [1, -1, 5, -2] sums to 3 and is the longest)

Example 2:

Given nums = [-2, -1, 2, 1]k = 1,
return 2. (because the subarray [-1, 2] sums to 1 and is the longest)

Follow Up:
Can you do it in O(n) time?

思路:O(n)算法

使用哈希表。记录下nums数组中从下标0到i之间所有数字的和。若该值为k,则更新res。若不等于,则检查该值减去k是否在哈希表中存在,若存在,则说明从之前的某一个位置到i之间的数字之和为k。我们用哈希表记录下每个和以及该和第一次出现时的i。

 class Solution {
public:
int maxSubArrayLen(vector<int>& nums, int k) {
vector<int> sum(nums.size(), );
unordered_map<int, int> help;
int tot = , res = ;
for (int i = , n = nums.size(); i < n; i++)
{
sum[i] = i == ? nums[] : sum[i - ] + nums[i];
if (sum[i] == k) res = i + ;
else if (help.count(sum[i] - k))
res = max(res, i - help[sum[i] - k]);
if (help.count(sum[i]) == )
help.insert(make_pair(sum[i], i));
}
return res;
}
};

Maximum Size Subarray Sum Equals k -- LeetCode的更多相关文章

  1. leetcode 560. Subarray Sum Equals K 、523. Continuous Subarray Sum、 325.Maximum Size Subarray Sum Equals k(lintcode 911)

    整体上3个题都是求subarray,都是同一个思想,通过累加,然后判断和目标k值之间的关系,然后查看之前子数组的累加和. map的存储:560题是存储的当前的累加和与个数 561题是存储的当前累加和的 ...

  2. Subarray Sum & Maximum Size Subarray Sum Equals K

    Subarray Sum Given an integer array, find a subarray where the sum of numbers is zero. Your code sho ...

  3. Subarray Sum & Maximum Size Subarray Sum Equals K && Subarray Sum Equals K

    Subarray Sum Given an integer array, find a subarray where the sum of numbers is zero. Your code sho ...

  4. [LeetCode] 325. Maximum Size Subarray Sum Equals k 和等于k的最长子数组

    Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If t ...

  5. [LeetCode] Maximum Size Subarray Sum Equals k 最大子数组之和为k

    Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If t ...

  6. [Locked] Maximum Size Subarray Sum Equals k

    Example 1: Given nums = [1, -1, 5, -2, 3], k = 3,return 4. (because the subarray [1, -1, 5, -2] sums ...

  7. Maximum Size Subarray Sum Equals k LT325

    Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If t ...

  8. LeetCode Maximum Size Subarray Sum Equals k

    原题链接在这里:https://leetcode.com/problems/maximum-size-subarray-sum-equals-k/ 题目: Given an array nums an ...

  9. LeetCode 325. Maximum Size Subarray Sum Equals k

    原题链接在这里:https://leetcode.com/problems/maximum-size-subarray-sum-equals-k/ 题目: Given an array nums an ...

随机推荐

  1. Spring整合EhCache详解

    一.EhCache介绍 EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider.Ehcache是一种广泛使用的开 源Java分布 ...

  2. python实现单链表的反转

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #!/usr/bin/env python #coding = utf-8 ...

  3. linux path环境变量基础

    系统环境变量与个人环境变量的配置文件 系统级别的配置文件:  /etc/profile :这个文件预设了几个重要的变量,例如PATH, USER, LOGNAME, MAIL, INPUTRC, HO ...

  4. java初学2

    1.数组操作类Arrays与System public static void arraycopy(Object src, int srcPos, Object dest,int destPos,in ...

  5. linux误删除恢复

    extundelete 大家基本都知道,在linux上误删除了东西后果是很严重的,尤其是在服务器上误删除了东西,对于字符终端,想要实现恢复删除的数据更是难上加难,对于Linux误删除了重要的东西,虽然 ...

  6. jquery实现各种实例

    1.正反选实例 <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  7. HDU 4699 Editor(双向链表)

    双向链表直接模拟. 用一个辅助数组maxSum来维护一下前k项中[1,k]的最大和. 因为光标是一格一格的移动,所以每次光标右移的时候动态更新一下即可. 时间复杂度O(n). #include < ...

  8. Could not automatically select an Xcode project. Specify one in your Podfile like so

    需要将Podfile文件放置在根目录下,而不能放置在项目的里面. 更改路径即可

  9. 【C++ 拾遗】C++'s most vexing parse

    C++'s most vexing parse 是 Scott Meyers 在其名著<Effective STL>中创造的一个术语. Scott 用这个术语来形容 C++ 标准对于 de ...

  10. sublime 设置代码片段不起作用的问题

    最近爱上了sublime 但是我也继续爱我的Vscode 安装sublime代码片段的时候,遇到了设置好的代码片段按  Tab建 不起作用的问题.快折磨死的时候灵光一闪: 首先检查设置的代码片段 &l ...