原题链接在这里: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:

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

Example 2:

  1. Input: nums = [-2, -1, 2, 1], k = 1
  2. Output: 2
  3. 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:

  1. class Solution {
  2. public int maxSubArrayLen(int[] nums, int k) {
  3. if(nums == null || nums.length == 0){
  4. return 0;
  5. }
  6.  
  7. int sum = 0;
  8. int res = 0;
  9.  
  10. HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>();
  11. hm.put(0, -1);
  12.  
  13. for(int i = 0; i<nums.length; i++){
  14. sum += nums[i];
  15. if(hm.containsKey(sum-k)){
  16. res = Math.max(res, i-hm.get(sum-k));
  17. }
  18.  
  19. if(!hm.containsKey(sum)){
  20. hm.put(sum, i);
  21. }
  22. }
  23. return res;
  24. }
  25. }

类似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使用正则表达式匹配多行 Pattern flags

    Java中正则匹配有多种模式,若不选择模式则默认为单行匹配 匹配模式(Pattern flags) compile()方法有两个模式 未开匹配模式 Pattern compile(String reg ...

  2. 10. Scala数据结构(上)-集合操作

    10.1 数据结构特点 10.1.1 Scala集合基本介绍 uml => 统一建模语言 1) Scala同时支持不可变集合和可变集合,不可变集合可以安全的并发访问 两个主要的包 不可变集合:s ...

  3. -UI调试工具 SAK 布局 MD

    目录 目录 SwissArmyKnife 接入方式 自动初始化版本 支持的功能 可配置项 原理 自定义功能 Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndro ...

  4. Kafka跨集群迁移方案MirrorMaker原理、使用以及性能调优实践

    序言Kakfa MirrorMaker是Kafka 官方提供的跨数据中心的流数据同步方案.其实现原理,其实就是通过从Source Cluster消费消息然后将消息生产到Target Cluster,即 ...

  5. Angular复习笔记6-依赖注入

    Angular复习笔记6-依赖注入 依赖注入(DependencyInjection)是Angular实现重要功能的一种设计模式.一个大型应用的开发通常会涉及很多组件和服务,这些组件和服务之间有着错综 ...

  6. python基础04--list,cou,dict

    1.1 列表list 1.列表可以完成大多数集合类的数据结构实现.列表中元素的类型可以不相同,它支持数字,字符串,列表,元组,集合,字典 2.列表是有序的, 可以索引,切片 3.List中的元素是可以 ...

  7. DOS常见命名整理

    Dos命令 1.打开终端的方式 (1).window+R / 点击开始栏,输入cmd,然后回车 (2).在某个文件夹下打开终端 按住shift+右击,有个在此处打开命令窗口 2.修改终端背景和文字颜色 ...

  8. Java 获取网络重定向URL(302重定向)

    方法1: import java.net.HttpURLConnection; import java.net.URL; import org.junit.Assert; import org.jun ...

  9. PHP实现单人多人聊天源码免费分享 | 电脑报修系统

    源码清单 1. 简易版登陆式聊天源码. 2. 电脑报修轻系统源码. 3. 关注下面公众号回复“聊天”,免费获取. 聊天系统 虽然微信,QQ是即时通讯的元老.但是他们限制很多,所以很多人都想做一个自己的 ...

  10. 浅谈Python设计模式 -- 责任链模式

    声明:本系列文章主要参考<精通Python设计模式>一书,并且参考一些资料,结合自己的一些看法来总结而来. 之前在最开始就聊了Python设计模式有三种,其中关于创建型和结构型设计模式基本 ...