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

题目:

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.

Note:
The sum of the entire nums array is guaranteed to fit within the 32-bit signed integer range.

Example 1:

Input: nums = [1, -1, 5, -2, 3], k = 3
Output: 4
Explanation: The subarray [1, -1, 5, -2] sums to 3 and is the longest.

Example 2:

Input: nums = [-2, -1, 2, 1], k = 1
Output: 2
Explanation: The subarray [-1, 2] sums to 1 and is the longest.

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

题解:

用HashMap记录<sum, index> pair. 遇到sum-k在HashMap中时说明从hm.get(sum-k) 到 i 这一段的和是k.

Time Complexity: O(n). n = nums.length.

Space: O(n).

AC Java:

 class Solution {
public int maxSubArrayLen(int[] nums, int k) {
if(nums == null || nums.length == 0){
return 0;
} int sum = 0;
int res = 0; HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>();
hm.put(0, -1); for(int i = 0; i<nums.length; i++){
sum += nums[i];
if(hm.containsKey(sum-k)){
res = Math.max(res, i-hm.get(sum-k));
} if(!hm.containsKey(sum)){
hm.put(sum, i);
}
}
return res;
}
}

类似Contiguous Array.

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

  1. [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 ...

  2. 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题是存储的当前累加和的 ...

  3. 【LeetCode】325. Maximum Size Subarray Sum Equals k 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 prefix Sum 日期 题目地址:https:// ...

  4. 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 ...

  5. 325. Maximum Size Subarray Sum Equals k

    最后更新 二刷 木有头绪啊.. 看答案明白了. 用的是two sum的思路. 比如最终找到一个区间,[i,j]满足sum = k,这个去见可以看做是 [0,j]的sum 减去 [0,i]的Sum. 维 ...

  6. 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 ...

  7. 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 ...

  8. [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 ...

  9. Maximum Size Subarray Sum Equals k -- LeetCode

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

随机推荐

  1. Java计算工作日的工具类

    有时候需要根据工作日计算指定的日期,也就是需要排除周六日. 1.  初版代码如下: package cn.xm.exam.utils; import java.util.Calendar; impor ...

  2. centos下 yum快速安装maven

    wget http://repos.fedorapeople.org/repos/dchen/apache-maven/epel-apache-maven.repo -O /etc/yum.repos ...

  3. vs2012新建单元测试

    多写单元测试也是算向优秀程序员迈进吧((ˇˍˇ)),就像我们小时候做算算术一样,老师会交给我们怎么样检验答案是否正确性.那么我们做程序员也一样,检验自己写的代码是否和我们预期的结果一样!项目小还行,但 ...

  4. asp.net core 系列之允许跨域访问-1(Enable Cross-Origin Requests:CORS)

    接上篇的允许跨域 4.CORS 策略(Policy)的选项 这里讲解Policy可以设置的选项: 设置允许的访问源 设置允许的HTTP methods 设置允许的请求头(request header) ...

  5. C语言----循环结构1(基础篇五)

    今天更新一个C语言的循环,简单点就是就是我们平时在使用电脑时不停的刷新网页,生活中每天都要吃饭等等就是循环,而编程中也有不断循环的过程,或者遇到符合的条件结束循环 下面进入今天的主题: 需求:用计算机 ...

  6. jwt认证生成后的token如何传回后端并解析的详解

    jwt认证生成后的token后端解析 一.首先前端发送token token所在的位置headers {'authorization':token的值',Content-Type':applicati ...

  7. python基础08--迭代器,生成器

    1.1 迭代器 1.可迭代对象:str,list,tuple,set,dict, 迭代器:f(文件),range() 可迭代对象和迭代器都可以for循环 可迭代对象不会计数, 迭代器会计数,上面操作到 ...

  8. drf--ModelSerializers序列化

    目录 drf--ModelSerializers序列化 项目准备 配置 settings.py 路由 多表设计 表关系分析 创建models 模型序列化 自定义模型序列化 api/serializer ...

  9. jQuery---jq操作标签文本(html(),text()),jq操作文档标签(插入,删除,修改),克隆,,jq操作属性,jq操作class属性,jq操作表单value,jq操作css,jq操作盒子(重要),jq操作滚动条

    jQuery---jq操作标签文本(html(),text()),jq操作文档标签(插入,删除,修改),克隆,,jq操作属性,jq操作class属性,jq操作表单value,jq操作css,jq操作盒 ...

  10. vs2017 添加 mysql EF实体数据模型闪退

    1.查看vs2017安装路径找到Mysql.Data.dll版本号与MySQL Connector Net版本是否一致 历史版本下载地址 http://mysql.inspire.net.nz/Dow ...