Enter an array of shapes with positive and negative elements, one or more contiguous elements as their sub-arrays, and a maximum of all sub-arrays

Source: Internet
Author: User

 Public classZhejiang { Public Static voidMain (string[] args) {int[] arr=New int[] {1,-2,3,10,-4,7,2,-5}; intI=getgreatestsum (arr);    System.out.println (i); }    Private Static intGetgreatestsum (int[] arr) {        if(arr==NULL|| Arr.length==0) {            return0; }        intGreatestsum=0; inttemp=greatestsum;  for(inti = 0; i < arr.length; i++) {Temp+=Arr[i]; if(temp<0{//if current and is negative, discard this value directly, should have positive number in array element, select any integer greater than negative temp=0; }            if(temp>greatestsum) {//Discover new Max and reassign it to Greatestsum greatestsum=temp; }        }        if(greatestsum==0{//Because many values are discarded, this extreme situation occurs, so the maximum value in a single element is finally assigned to Greatestsum Greatestsum=arr[0];  for(inti = 1; i < arr.length; i++) {                if(greatestsum<Arr[i]) {Greatestsum=Arr[i]; }                            }        }        returngreatestsum; }}

Enter an array of shapes with positive and negative elements, one or more contiguous elements as their sub-arrays, and a maximum of all sub-arrays

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.