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 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:
Given nums = [1, -1, 5, -2, 3]
, k = 3
,
return 4
. (because the subarray [1, -1, 5, -2]
sums to 3 and is the longest)
Example 2:
Given nums = [-2, -1, 2, 1]
, k = 1
,
return 2
. (because the subarray [-1, 2]
sums to 1 and is the longest)
Follow Up:
Can you do it in O(n) time?
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
以为只有max(max, a)才能取最长,不知道怎么写。其实就是一般思路 sum随着i增加而变长,把i给for一遍就行了。
[一句话思路]:
gap中一下子增加了k时,index也取对应值即可
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- map.containsKey(sum)没用,必须要sum == k才行
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
必须考虑中间的gap. gap中一下子增加了k时,index也取对应值即可
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
用常量sum才能累加,数组不能:
sum[i] += nums[i]; 相当于每个数都重新加了
[算法思想:递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
2 sum: hashmap
[代码风格] :
class Solution {
public int maxSubArrayLen(int[] nums, int k) {
//cc
if (nums == null || nums.length == 0) return 0; //ini: hashmap, max
HashMap<Integer, Integer> map = new HashMap<>();
int max = 0, sum = 0; //for loop: 3 conditions
for (int i = 0; i < nums.length; i++) {
sum += nums[i];
//contains, gap, not contains
if (sum == k) max = i + 1;
else if (map.containsKey(sum - k)) max = Math.max(max, i - map.get(sum - k)); if (!map.containsKey(sum)) map.put(sum, i);
} return max;
}
}
325. Maximum Size Subarray Sum Equals k 和等于k的最长子数组的更多相关文章
- 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 和等于k的最长子数组
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 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 prefix Sum 日期 题目地址:https:// ...
- LeetCode 325. Maximum Size Subarray Sum Equals k
原题链接在这里:https://leetcode.com/problems/maximum-size-subarray-sum-equals-k/ 题目: Given an array nums an ...
- 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 ...
- [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 ...
随机推荐
- qt下用启动图
void showSplash(void) { QSplashScreen*splash=newQSplashScreen; splash->setPixmap(QPixmap(":/ ...
- 数组与指针的区别,以及在STL中传递数组/指针
数组和指针在作为实参传入T[] 或T*的形参时没有区别 void f(int pi[]) { cout << sizeof(pi) << endl; } int a[5] = ...
- python 面向对象(三大特性)
python 面向对象(初级) (思维导图 ↑↑↑↑↑) 概述: 面向过程:根据业务逻辑从上到下垒代码. 函数式:将某功能代码封装至函数中,日后便无需重复编写,仅调用函数即可 面向对象:对函数进行分类 ...
- 第十章 hbase默认配置说明
hbase.rootdir:这个目录是region server的共享目录,用来持久化Hbase.URL需要是'完全正确'的,还要包含文件系统的scheme.例如,要表示hdfs中的 '/hbase ...
- 翻译内核uvcvideo.txt
翻译源:linux-3.4.20\Documentation\video4linux\uvcvideo.txt 译文: Linux USB视频类(UVC)驱动程序 ================== ...
- An Autofac Lifetime Primer
Or, “Avoiding Memory Leaks in Managed Composition” Understanding lifetime can be pretty tough when y ...
- oracle autotrace使用
通过以下方法可以把Autotrace的权限授予Everyone, 如果你需要限制Autotrace权限,可以把对public的授权改为对特定user的授权. D:\oracle\ora92>sq ...
- SVN目录结构
整理了一下svn目录结构,如下: 项目名称 ----branches 软件产品的迭代开发版本 ----tags 软件产品经过完整测试的历史稳定版本,已部署在客户机器上使用的 --- ...
- vs2010 安装 Ajax Control Toolkit
Ajax Control Toolkit 7.1005.0 The Ajax Control Toolkit contains a rich set of controls that you can ...
- ansible初识三
一.setup模块 ansible的 setup模块主要用来收集信息, 查看参数: [root@localhost ~]# ansible-doc -s setup # 查看参数,部分参数如下: fi ...