Subarray Sum

Given an integer array, find a subarray where the sum of numbers is zero. Your code should return the index of the first number and the index of the last number.

Notice

There is at least one subarray that it's sum equals to zero.

Example

Given [-3, 1, 2, -3, 4], return [0, 2] or [1, 3].

分析:

能够马上想到的答案是用两个for loop,找出从i 到 j 和为0的数。但是这里有一个更巧的方法。用一个array保存每个数和这个这个数之前的sum。

对于A = [-3, 1, 2, -3, 4], sum = [-3, -2, 0, -3, 1].

如果sum[j] - sum[i] = 0,那么我们就可以保证中间部分和为0.

 public class Solution {
public List<Integer> subarraySum(int[] nums) {
if (nums == null || nums.length < ) return null; List<Integer> list = new ArrayList<>();
int sum = ;
Map<Integer, Integer> map = new HashMap<>();
map.put(, -);
for (int i = ; i < nums.length; i++) {
sum += nums[i];
if (map.containsKey(sum)) {
int index = map.get(sum);
list.add(index + );
list.add(i);
return list;
} else {
map.put(sum, i);
}
}
return list;
}
}

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.

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)

分析:如果subarray[j ---- i]的和为K,那么sum[i] - sum[j - 1] = K.

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

Minimum Size Subarray Sum

Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn't one, return 0 instead.

Example:

Input: s = 7, nums = [2,3,1,2,4,3]
Output: 2
Explanation: the subarray [4,3] has the minimal length under the problem constraint.
 public class Solution {
public int minSubArrayLen(int s, int[] nums) {
if (nums == null || nums.length == ) return ;
int start = , total = ;
int minLength = Integer.MAX_VALUE;
for (int end = ; end < nums.length; end++) {
total += nums[end];
if (total >= s) {
minLength = Math.min(minLength, end - start + );
}
while (start <= end && total - nums[start] >= s ) {
total -= nums[start];
start++;
minLength = Math.min(minLength, end - start + );
}
} if (total < s) return ;
return minLength;
}
}

Subarray Sum Equals K

Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k.

Example 1:

Input:nums = [1,1,1], k = 2
Output: 2

Note:

  1. The length of the array is in range [1, 20,000].
  2. The range of numbers in the array is [-1000, 1000] and the range of the integer k is [-1e7, 1e7].
 public class Solution {
public int subarraySum(int[] nums, int k) {
int sum = , result = ;
Map<Integer, Integer> preSum = new HashMap<>();
preSum.put(, );
for (int i = ; i < nums.length; i++) {
sum += nums[i];
if (preSum.containsKey(sum - k)) {
result += preSum.get(sum - k);
}
preSum.put(sum, preSum.getOrDefault(sum, ) + );
}
return result;
}
}

fb: 如果给一组正数,看subarray和是否是一个数k,能否用o(n) + constant space解决?

答:可以,用两个指针,不断移动右指针,如果从坐指针到右指针的和大于k,移动坐指针。

转载请注明出处:cnblogs.com/beiyeqingteng/

Subarray Sum & Maximum Size Subarray Sum Equals K && Subarray Sum Equals K的更多相关文章

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

  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 560. Subarray Sum Equals K 、523. Continuous Subarray Sum、 325.Maximum Size Subarray Sum Equals k(lintcode 911)

    整体上3个题都是求subarray,都是同一个思想,通过累加,然后判断和目标k值之间的关系,然后查看之前子数组的累加和. map的存储:560题是存储的当前的累加和与个数 561题是存储的当前累加和的 ...

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

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

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

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

  8. 325. Maximum Size Subarray Sum Equals k

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

  9. LeetCode Partition to K Equal Sum Subsets

    原题链接在这里:https://leetcode.com/problems/partition-to-k-equal-sum-subsets/description/ 题目: Given an arr ...

随机推荐

  1. windows 系统无法安装

    1. 提示windows 无法安装到这个磁盘,选中的磁盘具有MBR分区表. Windows cannot be installed to this disk.the selected disk has ...

  2. CAN总线疑惑与解答

    1    CAN总线2根数据线是怎么表示数据信息1和0的? Can总线采用差分数据表示方法,平时2个数据线为2.5V,表示隐性(1).当用数据0(显性)需要发送时1跟数据线上升到3.5V另一个下降到1 ...

  3. css样式——选择器(三)

    https://www.cnblogs.com/haiyan123/p/7552235.html 1.怎么找到标签 2.如何操作标签的对象 一.css概述 CSS是Cascading Style Sh ...

  4. UML简单熟悉

    + :代表public - :代表private # :代表protected  实现,继承关系:implements,extends 关联关系:使一个类知道另一个类的属性和方法 每一个Driver类 ...

  5. hive hadoop 大数据初探

    一.环境搭建: 1.安装虚拟机,mac 系统推荐parallels:直接转化讲师发的xmdl后缀名的文件:里面自带了环境: 注意事项:mac 本机和虚拟机中centos 的通讯,需要修改centos中 ...

  6. springboot配置多环境

    https://www.cnblogs.com/jason0529/p/6567373.html   Spring的profiles机制,是应对多环境下面的一个解决方案,比较常见的是开发和测试环境的配 ...

  7. findbugs的使用

    我们通常都会在APP上线之后,发现各种错误,尤其是空指针异常,这些错误对于用户体验来说是非常不好的,但其实大部分的问题,我们都能够提前发现. 在编写代码的过程中,可能不会时时刻刻记得检查空的引用,还有 ...

  8. ElasticSearch文档操作介绍三

    ElasticSearch文档的操作 文档存储位置的计算公式: shard = hash(routing) % number_of_primary_shards 上面公式中,routing 是一个可变 ...

  9. ElasticSearch 索引整体迁移方案

    以下都是经过实战验证过的!!! [前提]使用相同的版本 ElasticSearch-5.5.1,只不过是在不同的服务器之间且重跑数据相对来说代价比较高,这种情况下就可以使用一下这种方式对索引整体迁移 ...

  10. selenium_采集药品数据2_采集所有表格

    Python爬虫视频教程零基础小白到scrapy爬虫高手-轻松入门 https://item.taobao.com/item.htm?spm=a1z38n.10677092.0.0.482434a6E ...