138. Subarray Sum "Lintcode,by java"

Source: Internet
Author: User
Tags lintcode

Description

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 last number of the index.

There is at least one subarray that it's sum equals to zero.

Example

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

Analysis: Given an array of topics, let you intercept and be a sub-array of 0, and return the subscript for the start and end of the subarray. First of all, I think, although the time complexity of the square level, but generally more easy to understand. The outer loop I represents the starting subscript of the subarray, and the inner loop represents the end subscript of the sub-array starting with I. During the cycle, as soon as the sum=0 is found, it jumps out of the loop and returns the result.

My code is as follows:

 Public classSolution {/**     * @paramnums:a List of integers *@return: A list of integers includes the index of the first number and the last number of the index*/     PublicList<integer> Subarraysum (int[] nums) {        //Write your code herelist<integer>list=NewArraylist<integer>(); if(nums.length==1&&nums[0]==0) {List.add (0); List.add (0); }         for(inti=0;i<nums.length-1;i++){            intSum=0;            List.clear ();            List.add (i);  for(intj=i;j<nums.length;j++) {sum+=Nums[j]; if(sum==0) {List.add (j); returnlist; }            }         }        returnlist; }}

On the Internet to see another way of thinking, linear level, feeling very good. Using a hash table, store the and, if any, of the two positions from the starting point to each position and equal, the number of all the numbers in the sub-array between these two numbers and is 0. A little around the mouth, for example.

There are the following arrays:

3,1,2,-1,2,2,1,-3,5 each location corresponds to:

3 4 6 5 7 9 10 7 13 There are two seven, then the middle sub-array is equivalent to no addition, that is, "2,1,-3" and 0. Put the following code:

 Public classSolution {/**     * @paramnums:a List of integers *@return: A list of integers includes the index of the first number * and the index of the last number*/     PublicArraylist<integer> Subarraysum (int[] nums) {        //Write your code here               intLen =nums.length; ArrayList<Integer> ans =NewArraylist<integer>(); HashMap<integer, integer> map =NewHashmap<integer, integer>(); Map.put (0,-1); intsum = 0;  for(inti = 0; i < Len; i++) {sum+=Nums[i]; if(Map.containskey (sum)) {Ans.add (Map.get (sum)+ 1);                Ans.add (i); returnans;        } map.put (sum, i); }               returnans; }}

138. Subarray Sum "Lintcode,by java"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.