Maximum Size Subarray Sum Equals k(2sum和Map)
Given an arraynums
and a target valuek
, find the maximum length of a subarray that sums to k. If there isn't one, return 0 instead.
Example
Given nums =[1, -1, 5, -2, 3]
, k =3
, return4
.
Given nums =[-2, -1, 2, 1]
, k =1
, return2
.
分析
2sum问题,presum存在map里,Map初始值是{0,-1} 因为presum相减之后,前数剪没了不含,所以坐标前补一位
比如presum 5-3其实只包含4,5 正好和长度一样。5-3长度是2
Last updated
Was this helpful?