LeetCode 325. 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 325. Maximum Size Subarray Sum Equals k的更多相关文章
- [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 ...
- 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题是存储的当前累加和的 ...
- 【LeetCode】325. Maximum Size Subarray Sum Equals k 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 prefix Sum 日期 题目地址:https:// ...
- 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 ...
- 325. Maximum Size Subarray Sum Equals k
最后更新 二刷 木有头绪啊.. 看答案明白了. 用的是two sum的思路. 比如最终找到一个区间,[i,j]满足sum = k,这个去见可以看做是 [0,j]的sum 减去 [0,i]的Sum. 维 ...
- 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] 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 ...
随机推荐
- C语言之四则运算表达式求值(链栈)—支持浮点型数据,负数, 整型数据运算
运算符间的优先级关系: 链栈结构体定义: 数据域使用字符串长度为20的字符数组(故需要注意判断读取的字符串是运算符还是数值) 可支持浮点型数据,负数, 整型数据的运算 float EvaluateE ...
- 从零开始学C语言
从零开始学C语言 @阆苑祁寒 更新时间:2019-09-13 写在前面:本文从一个初学者的角度,给出了对C语言的简单理解.如有谬误,敬请指出! Week1——基本语法 #include <std ...
- FileUpload上传多张照片
<asp:FileUpload ID="uploaderInput" runat="server" multiple="multiple&quo ...
- 记录一次使用NPOI遇到的问题
在.net 下一般使用NPOI操作Excel相信大家都不陌生,但是本人在操作过程中遇到一个比较奇怪的问题,特写此博客记录与大家分享. 例子是使用Winform,点击按钮时弹出打开文件对话框,然后选择文 ...
- 如何在Linux中复制文档
在办公室里复印文档过去需要专门的员工与机器.如今,复制是电脑用户无需多加思考的任务.在电脑里复制数据是如此微不足道的事,以致于你还没有意识到复制就发生了,例如当拖动文档到外部硬盘的时候. 数字实体复制 ...
- 安全SECUERITY英文SECUERITY证券
security Alternative forms secuerity (mostly obsolete) English Alternative forms secuerity Pronuncia ...
- Spring容器的refresh()介绍
Spring容器的refresh()[创建刷新]; 1.prepareRefresh()刷新前的预处理; 1).initPropertySources()初始化一些属性设置;子类自定义个性化的属性设置 ...
- Go语言入门——函数
写了快一个月的Go,已经没有什么陌生感了. 一行代码写完,不会在行尾本能的敲上“:”,直接Enter键换行: 定义变量把变量名放在前面,类型放在后面: 使用struct去定义自己想要的数据模型就好比定 ...
- centos7 安装php7扩展
安装php扩展(我用的php7.2版本) php是用amqp调用RabbitMQ,所以先下载ampq $ wget https://pecl.php.net/get/amqp-1.9.3.tgz #下 ...
- iview 多选下拉列表选项回显问题
如,简单的多选Select, <Select v-model="model" filterable clearable transfer multiple > < ...