Lintcode:subarray Sum

Source: Internet
Author: User
Tags lintcode

return The index of the first number and the index of the last number . Examplegiven[return [0, 2] or [1, 3].

Recommended solution: The idea was based on the prefix sum:iterate through the array and for every element array "i", calculate sum of Eleme NTS Form 0 To I (this can simply is done as sum + = arr "I"). If the current sum had been seen before, then there was a zero sum array, the start and end index is returned.

Use Hashmap:o (N) time, but more memory, big case will MLE

1  Public classSolution {2     /**3      * @paramnums:a List of integers4      * @return: A list of integers includes the index of the first number5 * The index of the last number6      */7      PublicArraylist<integer> Subarraysum (int[] nums) {8         //Write your code here9arraylist<integer> res =NewArraylist<integer>();Ten         if(nums==NULL|| nums.length==0)returnRes; OneHashmap<integer, integer> map =NewHashmap<integer, integer>(); AMap.put (0,-1); -         intsum = 0; -          for(inti=0; i<nums.length; i++) { theSum + =Nums[i]; -             if(!map.containskey (sum)) { - map.put (sum, i); -             } +             Else { -Res.add (Map.get (sum) +1); + Res.add (i); A                 returnRes; at             } -         } -         returnRes; -     } -}

Because the above concise code will be MLE, so (Nlog (n)) The second algorithm, a little more time, but less space

1 classElementImplementsComparable<element>{2     intindex;3     intvalue;4      PublicElement (intIintv) {5index =i;6Value =v;7     }8      Public intCompareTo (Element other) {9         return  This. value-Other.value;Ten     } One      Public intGetIndex () { A         returnindex; -     } -      Public intGetValue () { the         returnvalue; -     } - } -  +  Public classSolution { -     /** +      * @paramnums:a List of integers A      * @return: A list of integers includes the index of the first number at * The index of the last number -      */ -      PublicArraylist<integer> Subarraysum (int[] nums) { -arraylist<integer> res =NewArraylist<integer>(); -         if(nums==NULL|| nums.length==0)returnRes; -         intLen =nums.length; inElement[] sums =NewElement[len+1]; -Sums[0] =NewElement ( -1,0); to         intsum = 0; +          for(inti=0;i<len;i++){ -Sum + =Nums[i]; theSUMS[I+1] =NewElement (i,sum); *         } $ Arrays.sort (sums);Panax Notoginseng          for(inti=0;i<len;i++) -             if(Sums[i].getvalue () ==sums[i+1].getvalue ()) { the                 intStart = Math.min (Sums[i].getindex (), Sums[i+1].getindex ()) +1; +                 intEnd = Math.max (Sums[i].getindex (), sums[i+1].getindex ()); A Res.add (start); the Res.add (end); +                 returnRes; -             } $  $         returnRes; -     } -}

Lintcode:subarray Sum

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.