"Face question 31" continuous sub-array of the most Yamato

Source: Internet
Author: User

"Title description"

Enter an array of integers with positive numbers and negative numbers. A sub-array of one or more consecutive integers in the array that is the maximum value of all the sub-arrays.

Requires a time complexity of O (n).

"Solutions"

Solution One: an example of analyzing the law of arrays

My code implementation is for reference only:

1          Public Static intFindgreatestsumofsubarray (int[] arr)2         {3             if(arr = =NULL|| Arr. Length <1)4                 return-1;5 6             intmax = arr[0];7             intPrev = arr[0];8 9              for(inti =1; I < arr. Length; i++)Ten             { One                 if(Arr[i] + prev >Arr[i]) A                 { -Prev + =Arr[i]; -                 } the                 Else -                 { -Prev =Arr[i]; -                 } +  -max = prev > Max?Prev:max; +             } A  at             returnMax; -}

"Face question 31" continuous sub-array of the most Yamato

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.