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 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:
- The length of the array is in range [1, 20,000].
- 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的更多相关文章
- 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 ...
- [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 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] 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 ...
- 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 ...
- [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 ...
- 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 ...
- 325. Maximum Size Subarray Sum Equals k
最后更新 二刷 木有头绪啊.. 看答案明白了. 用的是two sum的思路. 比如最终找到一个区间,[i,j]满足sum = k,这个去见可以看做是 [0,j]的sum 减去 [0,i]的Sum. 维 ...
- LeetCode Partition to K Equal Sum Subsets
原题链接在这里:https://leetcode.com/problems/partition-to-k-equal-sum-subsets/description/ 题目: Given an arr ...
随机推荐
- MT【37】二次函数与整系数有关的题
解析: 评:两根式是不错的考虑方向,一方面二次函数两根式之前有相应的经验,另一方面这里$\sqrt{\frac{b^2}{4}-c}$正好和两个根有关系.
- Leetcode 26.删除排序数组中的重复项 By Python
给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成. 示例 1 ...
- 百度地图BMapLib.InfoBox 手机兼容源码修改
InfoBox.prototype.initialize = function (map) { var me = this; var div = this._div = baidu.dom.creat ...
- servlet3.0获取参数与文件上传代码示例
转: servlet3.0获取参数与文件上传代码示例 2018年08月26日 20:25:35 苏凯勇往直前 阅读数:98 package com.igeek.servlet; import ...
- mysql 多表管理修改
update t_res_ys,cms_article_data,cms_article set cms_article_data.jsdata=t_res_ys.jsdata ,cms_articl ...
- js的各种验证
验证手机号格式是否正确 // 判断是否为手机号 isPoneAvailable: function (pone) { var myreg = /^[1][3,4,5,7,8][0-9]{9}$/; i ...
- Spark记录-大数据简介
什么是大数据 大数据(big data),指无法在一定时间范围内用常规软件工具进行捕捉.管理和处理的数据集合,是需要新处理模式才能具有更强的决策力.洞察发现力和流程优化能力的海量.高增长率和多样化的信 ...
- ActiveMQ进阶配置
配置web管理页面的安全认证 配置web管理页面的绑定IP和端口 配置MQ连接的安全认证 禁用不使用的连接协议 绑定协议连接端口到指定IP 使用MySql作为持久化保存 配置基于JDBC的高可用环境 ...
- linux的基本操作与常见命令
linux的基本操作与常见命令: jdk的安装: 步骤:(特别注意:虚拟机安装的一般是32位的操作系统,jdk也必须使用32位的) 查看虚拟机版本:sudo uname --m i686 //表示是3 ...
- postgresql行转列
问:怎么分页&&按条件&&按顺序&&姓名不重复查出数据? 答:其实就是行转列,那么,postgresql怎么进行转列呢,百度了下,大概有三种写法 写法1 ...