[抄题]:

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也取对应值即可

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 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的最长子数组的更多相关文章

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

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

  5. 325. Maximum Size Subarray Sum Equals k

    最后更新 二刷 木有头绪啊.. 看答案明白了. 用的是two sum的思路. 比如最终找到一个区间,[i,j]满足sum = k,这个去见可以看做是 [0,j]的sum 减去 [0,i]的Sum. 维 ...

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

随机推荐

  1. oracle之 sqlplus prelim 参数介绍 ( 处理hang )

    从Oracle10g开始,sqlplus提供了一个参数选项-prelim,用这个参数,在系统已经hang的时候.我们可以连接到SGA而不是数据库,也就是说没有session被创建. 一. 通过以下步骤 ...

  2. 学习 Git 玩转 GitHub

    原文地址:学习 Git 玩转 GitHub 博客地址:http://www.extlight.com 一.基本了解 1.1 什么是版本控制系统 版本控制系统是一种记录一个或若干个文件内容变化,以便将来 ...

  3. erlang的格式化字符串

    往pgsql里面写数据的时候,不能双引号,开始纠结的不行,用拼字符串的形式,后来发现可以格式化字符串,泪奔 data_format.erl -module(data_format). -export( ...

  4. VC编译选项 md /mdd /ml /mt/mtd

    VC编译选项 多线程(/MT)多线程调试(/MTd)多线程 DLL (/MD)多线程调试 DLL (/MDd)C 运行时库                        库文件Single threa ...

  5. struts2学习(14)struts2文件上传和下载(4)多个文件上传和下载

    四.多个文件上传: 五.struts2文件下载: 多个文件上传action com.cy.action.FilesUploadAction.java: package com.cy.action; i ...

  6. zookeeper实战:SingleWorker代码样例

    我们需要一个“单点worker”系统,此系统来确保系统中定时任务在分布式环境中,任意时刻只有一个实例处于活跃:比如,生产环境中,有6台机器支撑一个应用,但是一个应用中有30个定时任务,这些任务有些必须 ...

  7. Oracle清理大表,降水位

    背景:一张表的清理机制存在问题,导致该表的数据一直在增加,该表水位已很高,需要对该表的数据进行清理并降水位. 1.1 迁移前准备 步骤一.新建表 p_transaction_bak. oracle@l ...

  8. 命令行创建2003的IP安全策略

      IP安全策略从win2k到2003都有的,图形界面的没什么好说的,如何在命令行下控制IPSec呢?win2k的方法在Do All in Cmd Shell有介绍.这里就拿win2003做例子吧,毕 ...

  9. node的express中间件之directory

    direcotry中间件用于在浏览器中流出网站某个目录下的所有子目录及文件. app.use(express.directory(path,[options])); 查看网站根目录下的文件及目录 va ...

  10. quartz报错 Couldn't retrieve job because the BLOB couldn't be deserialized: null

    今天线上添加定时任务之后 定时任务查询页面报出如上错误, 原因有两点 1.org.quartz.jobStore.useProperties = true 这个属性的意思存储的JobDataMaps是 ...