最后更新

二刷

木有头绪啊。。

看答案明白了。

用的是two sum的思路。

比如最终找到一个区间,[i,j]满足sum = k,这个去见可以看做是

[0,j]的sum 减去 [0,i]的Sum.

维护一个map,来记录0-i的和,我们希望

0~i + k = 0 ~ j ,这样就可以更新一次i~j的区间长度作为候补的结果。

Time: O(n)

Space: O(n)

public class Solution {
public int maxSubArrayLen(int[] nums, int k) {
if (nums.length == 0) return 0;
Map<Integer,Integer> map = new HashMap<>(); int sum = 0;
int res = 0; for (int i = 0; i < nums.length; i++) {
sum += nums[i];
if (sum == k) {
res = Math.max(res, i + 1);
} else if (map.containsKey(sum - k)) {
res = Math.max(res, i - map.get(sum - k));
} if (!map.containsKey(sum)) {
map.put(sum, i);
}
} return res; }
}

一刷

今晚各种卡。看提示是MAP,有了头绪。

先计算从0-0 0-1 0-2 0-3 0-4 0-n的值,累加就行,存入map,注意key是累加的值,value是0-index的indx。那么可能会有重复穿线的值,比如0-4 0-8的累加结果都是6,我们取最后的,一会解释。。

构建完MAP之后查MAP,看看有没有哪个值是target,有就更新一下结果= map.get(k) - 0,-0因为当前所有值都是从0开始累加的。

然后遍历数组。

看看1-1 1-2 1-3 1-5 .. 1-n有没有值等于target + nums[0],有就更新

再2-2 2-3 2-4 2-5 ... 2-n == target + nums[0] + nums[1]。。有就更新

解释一开始说的,为什么选后出现的INDEX作为value.

是因为我们倾向于选择晚出现的,因为如果1个值被查到,长度是返还的INDEX-当前的I,肯定是INDEX越大越符合我们要求,所以INDEX取晚出现的。

刚才讲过程的时候发现问题了。。好像没必要遍历两次,第二次查表的时候需要查的值越来越少,0-n 1-n 2-n,虽然查表都是O(1)不影响,但是似乎一遍就能下来。

假如结果是 1-5 减去1-3,在一开始建表的时候就可以计算,而且他的结果不会被后面的计算影响。 而6-8的结果只会从1-8 - 1-6得到,或者先从这个地方得到,不存在以后又遇到,就算以后又出现,肯定是1-8返还的index减去第一次出现的1-6得到的结果最大。。

好像可以,二刷再说。。先放个2 PASS的,反正都是O(N)

public class Solution
{
public int maxSubArrayLen(int[] nums, int k)
{
Map<Integer,Integer> map = new HashMap<Integer,Integer>(); int sum = 0;
for(int i = 0; i < nums.length;i++)
{
sum += nums[i];
map.put(sum,i);
} int res = 0;
if(map.containsKey(k))
res = map.get(k)+1; for(int i = 0; i < nums.length;i++)
{
if(map.containsKey(k+nums[i]))
res = Math.max(res,map.get(k+nums[i]) - i); k += nums[i];
} return res;
}
}

325. Maximum Size Subarray Sum Equals k的更多相关文章

  1. 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题是存储的当前累加和的 ...

  2. [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 ...

  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. LeetCode 325. Maximum Size Subarray Sum Equals k

    原题链接在这里:https://leetcode.com/problems/maximum-size-subarray-sum-equals-k/ 题目: Given an array nums an ...

  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. 杭电ACM(1002) -- A + B Problem II 大数相加 -提交通过

    杭电ACM(1002)大数相加 A + B Problem II Problem DescriptionI have a very simple problem for you. Given two ...

  2. oracle connect by 和start with

    网上找了个例子 测试了一下 貌似明白了create table t2(root_id number,id number,name varchar(5),description varchar(10)) ...

  3. !! UML十四图打油诗记忆法

    http://www.cnitpm.com/pm/7458.html UML十四图打油诗记忆法 UML十四图打油诗记忆法 UML它有十四图 包含静态和动态(分类) 类图构件搞对象(类图.构件图.对象图 ...

  4. 初始化一台linux server来做项目管理和测试

    毕业以后很多没做过这么技术的事情了,不过今年要开始咯. Goal: 练手安装Nginx,并且配置不同的server,后端有Tomcat的(JIRA),有PHP(总得有的),还有Tornado和Node ...

  5. bzoj1061 1283

    以前写的1061但一直没懂,后来懂了但忘写解题报告了 做了1283顺便补一下吧 1061 我是orz https://www.byvoid.com/blog/noi-2008-employee/#mo ...

  6. php简单实现MVC

    在PHP中使用MVC越来越流行了,特别是在一些开源的框架当中.MVC足以应对大多数的情况,但还有一些情况是其不太适合的,如比较简单的个人博客,对于只有几百篇文章量级的博客,使用MVC让人觉得有些太复杂 ...

  7. nginx多域名的配置方法

    方法一:多个.conf方法 1. 到/usr/local/nginx/ 新建一个目录 vhosts  并创建两个conf文件,如:wodejj.com.conf ,xiaobing.com.conf. ...

  8. Java [Leetcode 283]Move Zeroes

    题目描述: Given an array nums, write a function to move all 0's to the end of it while maintaining the r ...

  9. Java [leetcode 28]Implement strStr()

    题目描述: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...

  10. Java [leetcode 20]Valid Parentheses

    题目描述: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if th ...