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 Map<Integer,Integer> map = new HashMap<>(); int sum = 0; int max = 0; for(int i = 0;i < arr.length;i++){ sum += arr[i]; if(sum == k) { max = max > i+1 ? max:i+1; continue; } if(!map.containsKey(sum)){ map.put(sum,i); } if(map.containsKey(sum - k)){ max = max > i - map.get(sum-k) ? max :i - map.get(sum-k); } } return max; } }