LintCode-Subarray Sum

LiBlog發表於2014-12-27

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

Given [-3, 1, 2, -3, 4], return [0, 2] or [1, 3].

Solution 1 (nlog(n)):

 1 class Element implements Comparable<Element>{
 2     int index;
 3     int value;
 4     public Element(int i, int v){
 5         index = i;
 6         value = v;
 7     }
 8     public int compareTo(Element other){
 9         return this.value-other.value;
10     }
11     public int getIndex(){
12         return index;
13     }
14     public int getValue(){
15         return value;
16     }
17 }
18 
19 public class Solution {
20     /**
21      * @param nums: A list of integers
22      * @return: A list of integers includes the index of the first number
23      *          and the index of the last number
24      */
25     public ArrayList<Integer> subarraySum(int[] nums) {
26         ArrayList<Integer> res = new ArrayList<Integer>();
27         if (nums==null || nums.length==0) return res;
28         int len = nums.length;
29         Element[] sums = new Element[len+1];
30         sums[0] = new Element(-1,0);
31         int sum = 0;
32         for (int i=0;i<len;i++){
33             sum += nums[i];
34             sums[i+1] = new Element(i,sum);
35         }
36         Arrays.sort(sums);
37         for (int i=0;i<len;i++)
38             if (sums[i].getValue()==sums[i+1].getValue()){
39                 int start = Math.min(sums[i].getIndex(),sums[i+1].getIndex())+1;
40                 int end = Math.max(sums[i].getIndex(),sums[i+1].getIndex());
41                 res.add(start);
42                 res.add(end);
43                 return res;
44             }
45 
46         return res;
47     }
48 }

Solution 2 ( n, but more memory):

 1 public class Solution {
 2     /**
 3      * @param nums: A list of integers
 4      * @return: A list of integers includes the index of the first number
 5      *          and the index of the last number
 6      */
 7     public ArrayList<Integer> subarraySum(int[] nums) {
 8         ArrayList<Integer> res = new ArrayList<Integer>();
 9         if (nums==null || nums.length==0) return res;
10         int len = nums.length;
11         Map<Integer,List<Integer>> map = new HashMap<Integer,List<Integer>>();
12         List<Integer> aList = new ArrayList<Integer>();
13         aList.add(-1);
14         map.put(0,aList);
15         int sum =0;
16         for (int i=0;i<len;i++){
17             sum += nums[i];
18             //check the exists of current sum.
19             if (map.containsKey(sum)){
20                 int start = map.get(sum).get(0)+1;
21                 res.add(start);
22                 res.add(i);
23                 return res;
24             } else {
25                 aList = new ArrayList<Integer>();
26                 aList.add(i);
27                 map.put(sum,aList);
28             }
29         }
30 
31         return res;
32     }
33 }