题解 | #和为K的连续子数组#
和为K的连续子数组
https://www.nowcoder.com/practice/704c8388a82e42e58b7f5751ec943a11
import java.util.*; public class Solution { /** * max length of the subarray sum = k * @param arr int整型一维数组 the array * @param k int整型 target * @return int整型 */ public int maxlenEqualK (int[] arr, int k) { // write code here if (arr == null || arr.length < 1) { return 0; } Map<Integer, Integer> map = new HashMap<>(); map.put(0, -1); int len = 0; int sum = 0; for (int i = 0; i < arr.length; i++) { sum += arr[i]; if (map.containsKey(sum - k)) { len = Math.max(len, i - map.get(sum - k)); } if (!map.containsKey(sum)) { map.put(sum, i); } } return len; } }