标签:== span key stat contain package sum mat code
package com.hzins.suanfa; import java.util.HashMap; public class demo { /** * 数组中累加和为k的最大子数组的长度 * @param arr * @param k * @return */ public static int maxLengh(int[] arr,int k){ if(arr == null || arr.length == 0){ return 0; } HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); map.put(0, -1); int len = -1; int sum = 0; for(int i=0 ;i < arr.length;i++){ sum += arr[i]; if(map.containsKey(sum - k)){ len = Math.max(i - map.get(sum - k), len); } if(map.containsKey(sum)){ map.put(sum, i); } } return len; } }
标签:== span key stat contain package sum mat code
原文地址:http://www.cnblogs.com/caobojia/p/6785297.html