Offer to prove safety study notes (C #) - A maximum continuous sub-arrays and

Title Description

HZ occasionally get some professional issues to flicker those non-computer science students. After the test group will finish today, he spoke up: in the old one-dimensional pattern recognition, it is often necessary to calculate the maximum and continuous sub-vector when the vector is a positive whole number, the problem solved. However, if the vector contains a negative number, it should contain a negative number, and expect a positive number next to it would make up for it? For example: {6, -3, -2,7, -15,1,2,2}, and the maximum successive sub-vectors of 8 (beginning from 0, up to the third). To an array and returns its maximum continuous subsequence and you will not be fooled him live? (Sub-vector length is at least 1)

A. Subject analysis

        Above subject is too complicated, so I asked him to become a law: Enter an integer array, the array has a positive but also negative. A continuous array or a plurality of sub-array of integers. Seeking the maximum of all the sub-arrays and. Required time complexity is O (n-) .

        In short, an array Well, is divided into sub-arrays and a complete array of this topic will be: The largest sub-array this array if there are negative I this array to find out. For example, the input array is {1, -2, 3,10, -4,7,2 , -5}, and the largest of {3,10 subarray, -4,7,2}, the output for that sub and an array of 18.

        practice:

II. Code implementation

class Solution 
{ 
    public  int FindGreatestSumOfSubArray ( int [] Array) 
    { 
        // Write code here Wallpaper
         // robust determination 
        IF (array.length == 0 )
         return  0 ;
         // define a constant and even add operation constant maximum value max (with in the final output) 
        int max = array [ 0 ];
         int a = 0 ;
         // loop through 
        for ( int I = 0 ; I <array.length; I ++ ) 
        { 
            // input a constant array + a> constant the input , coupled with his 
            if(Array [I] + a> Array [I]) 
            { 
                a + = Array [I]; 
            } 
            // Otherwise, a is equal to him 
            the else 
            { 
                a = Array [I]; 
            } 
            // final output determination 
            IF (a> max) 
            { 
                max = A; 
            } 
        } 
        return max; 
    } 
}

 

Guess you like

Origin www.cnblogs.com/WeiMLing/p/10963324.html