[LeetCode]Maximum Size Subarray Sum Equals k

O(n)时间空间复杂度

public class Solution {
    public int maxSubArrayLen(int[] nums, int k) {
        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
        int sum = 0;
        int result = 0;
        map.put(0, -1);
        for (int i = 0; i < nums.length; i++) {
            sum += nums[i];
            if (map.containsKey(sum - k)) {
                result = Math.max(result, i - map.get(sum - k));
            }
            if (!map.containsKey(sum)) {
                map.put(sum, i);
            }
        }
        return result;
    }
}

 

posted @ 2016-01-05 15:31  Weizheng_Love_Coding  阅读(205)  评论(0编辑  收藏  举报