解题思路: HashMap和前缀和 ,将前缀和存进HashMap中
public static int maxSubArrayLen(int[] nums, int k) {
// Write your code here
Map<Integer, Integer> map = new HashMap<>(); // sum -> id
map.put(0, -1);
int maxLen = 0;
for (int i = 0, sum = 0; i < nums.length; i++) {
sum += nums[i];
if (map.containsKey(sum - k)) {
maxLen = Math.max(maxLen, i - map.get(sum - k));
} else {
map.putIfAbsent(sum, i);
}
}
return maxLen;
}