Subarray Sum

Linc 138

Given an integer array, find a subarray where the sum of numbers is zero. Your code should return the index of the first number and the index of the last number.

Example Example 1: Input: [-3, 1, 2, -3, 4] Output: [0, 2] or [1, 3].

Explanation:
return anyone that the sum is 0.

Example 2: Input: [-3, 1, -4, 2, -3, 4] Output: [1,5]

Notice: 这个题的testcase不允许我重复利用array来存sum,因为他最后估计unit test又用了这个array来看是不是0.

public class Solution {
    /**
     * @param nums: A list of integers
     * @return: A list of integers includes the index of the first number and the index of the last number
     */
    public List<Integer> subarraySum(int[] nums) {
        List<Integer> res = new ArrayList<>();
        if (nums == null || nums.length == 0) return res;
        Map<Integer, Integer> map = new HashMap<>();
        map.put(0, -1);
        int sum = 0;
        for (int i = 0; i < nums.length; i++) {
            sum +=nums[i];
            if (map.containsKey(sum)) {
                res.add(map.get(sum) + 1);
                res.add(i);
                return res;
            }
            map.put(sum, i);
        }

        return res;
    }
}

results matching ""

    No results matching ""