LeetCode Maximum Size Subarray Sum Equals k
原题链接在这里: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;
}
}
LeetCode Maximum Size Subarray Sum Equals k的更多相关文章
- [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 ...
- 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题是存储的当前累加和的 ...
- 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 ...
- 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 ...
- [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 ...
- 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 ...
- [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 ...
- 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 ...
- LeetCode 325. Maximum Size Subarray Sum Equals k
原题链接在这里:https://leetcode.com/problems/maximum-size-subarray-sum-equals-k/ 题目: Given an array nums an ...
随机推荐
- 厉害了,摩托罗拉发布全球首款支持VR和AR的手机MotoZ
目前支持谷歌daydream移动VR生态系统的手机型号并不多,除了谷歌自家的Pixel系列外,还有华为推出的mate9系列.如今又到了一位新成员,即升级到Android 7.0后的Moto Z. 更为 ...
- [SDOI2011]染色 题解
题目大意: 给定一棵有n个节点的无根树和m个操作,操作有2类: 1.将节点a到节点b路径上所有点都染成颜色c: 2.询问节点a到节点b路径上的颜色段数量(连续相同颜色被认为是同一段) 思路: 树剖之后 ...
- Redis的两个小技巧
1.通配删除相同前缀的缓存 DEL命令的参数不支持通配符,但我们可以结合Linux的管道和xargs命令自己实现删除所有符合规则的键. 比如要删除所有以“structure_”开头的键,就可以执行下面 ...
- ACM 荷兰国旗问题
荷兰国旗问题 时间限制:3000 ms | 内存限制:65535 KB 难度:1 描述 荷兰国旗有三横条块构成,自上到下的三条块颜色依次为红.白.蓝.现有若干由红.白.蓝三种颜色的条块序列,要 ...
- JSP 基础概念归纳 5分钟看完
1. 符合 j2ee 标准的 web-app 的目录结构 WEB-INF classes web.xml lib servlet 开发过程 从 httpservlet 继承, 重写 doget / d ...
- java8 引进lamda
就像泛型能使开发人员对数据类型进行抽象,Lambda的目的是让程序员能够对程序行为进行抽象. 你可以这样想,它能够让程序员把一段程序代码当做数据一样使用.一个方法可以像定义和使用一个变量那样的方式被定 ...
- BZOJ1110: [POI2007]砝码Odw
Description 在byteotian公司搬家的时候,他们发现他们的大量的精密砝码的搬运是一件恼人的工作.公司有一些固定容量的容器可以装这些砝码.他们想装尽量多的砝码以便搬运,并且丢弃剩下的砝码 ...
- BZOJ3293: [Cqoi2011]分金币
Description 圆桌上坐着n个人,每人有一定数量的金币,金币总数能被n整除.每个人可以给他左右相邻的人一些金币,最终使得每个人的金币数目相等.你的任务是求出被转手的金币数量的最小值. Inpu ...
- Notification Once
Notification Once 前段时间整理项目中的AppDelegate,发现很多写在- application:didFinishLaunchingWithOptions:中的代码都只是为了在 ...
- 分模块创建maven项目(一)
maven是一个项目构建和管理的工具. 我们可以通过maven仓库可以实现管理构建(主要是JAR还包括:WAR,ZIP,POM等等). 我们可以通过maven插件可以实现编译源代.产生Javadoc文 ...