560. Subarray Sum Equals K & Subarray Sum & Maximum Size Subarray Sum Equals K & Minimum Size Subarray Sum & Subarray Sum Equals K
560. Subarray Sum Equals K
Given an array of integers nums
and an integer k
, return the total number of subarrays whose sum equals to k
.
A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Input: nums = [1,1,1], k = 2 Output: 2
Example 2:
Input: nums = [1,2,3], k = 3 Output: 2
Constraints:
1 <= nums.length <= 2 * 104
-1000 <= nums[i] <= 1000
-107 <= k <= 107
1 class Solution { 2 public int subarraySum(int[] nums, int k) { 3 int sum = 0, result = 0; 4 Map<Integer, Integer> preSum = new HashMap<>(); 5 preSum.put(0, 1); 6 for (int i = 0; i < nums.length; i++) { 7 sum += nums[i]; 8 result += preSum.getOrDefault(sum - k, 0); 9 preSum.put(sum, preSum.getOrDefault(sum, 0) + 1); 10 } 11 return result; 12 } 13 }
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.
1 public class Solution { 2 public List<Integer> subarraySum(int[] nums) { 3 if (nums == null || nums.length < 1) return null; 4 5 List<Integer> list = new ArrayList<>(); 6 int sum = 0; 7 Map<Integer, Integer> map = new HashMap<>(); 8 map.put(0, -1); 9 for (int i = 0; i < nums.length; i++) { 10 sum += nums[i]; 11 if (map.containsKey(sum)) { 12 int index = map.get(sum); 13 list.add(index + 1); 14 list.add(i); 15 return list; 16 } else { 17 map.put(sum, i); 18 } 19 } 20 return list; 21 } 22 }
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.
1 public class Solution { 2 public int maxSubArrayLen(int[] nums, int k) { 3 if (nums == null || nums.length == 0) { 4 return 0; 5 } 6 7 int maxLen = 0; 8 Map<Integer, Integer> map = new HashMap<>(); 9 map.put(0, -1); 10 int sum = 0; 11 12 for (int i = 0; i < nums.length; i++) { 13 sum += nums[i]; 14 if (!map.containsKey(sum)) { 15 map.put(sum, i); 16 } 17 18 if (map.containsKey(sum - k)) { 19 maxLen = Math.max(maxLen, i - map.get(sum - k)); 20 } 21 } 22 return maxLen; 23 } 24 }
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.
1 public class Solution { 2 public int minSubArrayLen(int s, int[] nums) { 3 if (nums == null || nums.length == 0) return 0; 4 int start = 0, total = 0; 5 int minLength = Integer.MAX_VALUE; 6 for (int end = 0; end < nums.length; end++) { 7 total += nums[end]; 8 if (total >= s) { 9 minLength = Math.min(minLength, end - start + 1); 10 } 11 while (start <= end && total - nums[start] >= s ) { 12 total -= nums[start]; 13 start++; 14 minLength = Math.min(minLength, end - start + 1); 15 } 16 } 17 18 if (total < s) return 0; 19 return minLength; 20 } 21 }
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].
1 public class Solution { 2 public int subarraySum(int[] nums, int k) { 3 int sum = 0, result = 0; 4 Map<Integer, Integer> preSum = new HashMap<>(); 5 preSum.put(0, 1); 6 for (int i = 0; i < nums.length; i++) { 7 sum += nums[i]; 8 if (preSum.containsKey(sum - k)) { 9 result += preSum.get(sum - k); 10 } 11 preSum.put(sum, preSum.getOrDefault(sum, 0) + 1); 12 } 13 return result; 14 } 15 }
fb: 如果给一组正数,看subarray和是否是一个数k,能否用o(n) + constant space解决?
答:可以,用两个指针,不断移动右指针,如果从坐指针到右指针的和大于k,移动坐指针。
转载请注明出处:cnblogs.com/beiyeqingteng/