原题链接在这里: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. 模拟 + 打表 --- Emag eht htiw Em Pleh

    Emag eht htiw Em Pleh Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2578   Accepted: ...

  2. CSP2019退役记

    写在前面 all last,我又失败了,我退役了 回忆我这个菜鸡OI生涯,有看机房神仙切题的乐趣,也有自己考场爆炸的辛酸 NOIP2017,我第一次参赛,我pj205二等打铁 NOIP2018,我第二 ...

  3. Java学习:异常的概念

    异常 异常概念 异常:指的是程序在执行过程中,出现的非正常的情况,最终导致JVM的非正常停止. 在Java等面向对象的编程语言中,异常本身是一个类,产生异常就是创建异常对象并抛出一个异常对象.Java ...

  4. C#中字符串转换为计算公式

    添加COM引用: private void button_Click(object sender, EventArgs e) { MSScriptControl.ScriptControl sc = ...

  5. SQL 查询表外键_T-Sql 2016——级联删除外键查询

    SELECT fk.name AS foreign_key_name, oSub.name AS table_name, SubCol.name AS table_column, oMain.name ...

  6. 2019 草花手游java面试笔试题 (含面试题解析)

      本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.草花手游等公司offer,岗位是Java后端开发,因为发展原因最终选择去了草花手游,入职一年时间了,也成为了面 ...

  7. JS判断是否是数组的四种做法(转载)

    转载来源 https://www.cnblogs.com/echolun/p/10287616.html 一.前言 如何判断一个对象或一个值是否是一个数组,在面试或工作中我们常常会遇到这个问题,既然出 ...

  8. backtrace() returns only one stack frame

    参考: 在Linux中如何利用backtrace信息解决程序崩溃的问题 linux 打印堆栈方法 https://devtalk.nvidia.com/default/topic/987279/jet ...

  9. k8s运维处理

    k8s运维处理 驱逐节点容器,进行docker,等重要组件的重启时,打驱逐标记 kubectl drain [option --node ip] 进行重启docker或kubelet等其他操作,操作完 ...

  10. Httpd服务进阶知识-基于Apache Modele的LAMP架构之PhpMyAdmin案例

    Httpd服务进阶知识-基于Apache Modele的LAMP架构之PhpMyAdmin案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.常见LAMP应用 PhpMyAdm ...