LintCode-Subarray Sum

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 }
时间: 2024-10-14 00:40:59

LintCode-Subarray Sum的相关文章

[LintCode] Subarray Sum &amp; Subarray Sum II

Subarray Sum 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]. 用hashmap来存从nums[0

[LintCode] Subarray Sum II

Given an integer array, find a subarray where the sum of numbers is in a given interval. Your code should return the number of possible answers. (The element in the array should be positive) Example Given [1,2,3,4] and interval = [1,3], return 4. Bra

Leetcode: Maximum Size Subarray Sum Equals k

Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If there isn't one, return 0 instead. Example 1: Given nums = [1, -1, 5, -2, 3], k = 3, return 4. (because the subarray [1, -1, 5, -2] sums to 3 and is th

Continuous Subarray Sum II(LintCode)

Continuous Subarray Sum II Given an circular integer array (the next element of the last element is the first element), find a continuous subarray in it, where the sum of numbers is the biggest. Your code should return the index of the first number a

560. Subarray Sum Equals K

Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k. Example 1: Input:nums = [1,1,1], k = 2 Output: 2 Note: The length of the array is in range [1, 20,000]. The range of numbers

Subarray Sum Closet

Given an integer array, find a subarray with sum closest to zero. Return the indexes of the first number and last number. Given [-3, 1, 1, -3, 5], return [0, 2], [1, 3], [1, 1], [2, 2] or [0, 4]. 这题求和最接近0的子数组,属于Subarray Sum的follow up.思路也很近似,每次求的是当前位置

Subarray Sum Closest

Question Given an integer array, find a subarray with sum closest to zero. Return the indexes of the first number and last number. Given [-3, 1, 1, -3, 5], return [0, 2], [1, 3],[1, 1], [2, 2] or [0, 4]. Answer 这道题延续Subarray Sum的思路,即将[0, i]的sum存起来.这里

LeetCode OJ 209. Minimum Size Subarray Sum

Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead. For example, given the array [2,3,1,2,4,3] and s = 7,the subarray [4,3] has the minimal

[LeetCode] Minimum Size Subarray Sum 解题思路

Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead. For example, given the array [2,3,1,2,4,3] and s = 7,the subarray [4,3] has the minimal

leetcode_209题——Minimum Size Subarray Sum(两个指针)

Minimum Size Subarray Sum Total Accepted: 10318 Total Submissions: 44504My Submissions Question Solution Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, r