Topic
Describe:
输入一个整形数组。数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和。求所有子数组的和的最大值。
Interface
Int GetSubArraySum(Int* pIntArray,Int nCount);
Specifications
要求时间复杂度为O(n)
Example
例如输入的数组为1, -2, 3, 10, -4, 7, 2, -5,和最大的子数组为3, 10, -4, 7, 2,因此输出为该子数组的和18
Practice Stage:
Code
/* ---------------------------------------* Date: 2015-07-05* sjf0115* title: The maximum and * source of the Subarray: Huawei Machine Test Exercises-------------------- ---------------------*/#include <iostream>#include <string.h>#include "oj.h"using namespace Std;/ * Function: Input: Pintarray: Array, ncout: array length output: return: Maximum value * /intGetsubarraysum (int* Pintarray,intncount) {if(Pintarray = = NULL | | ncount <=0){return 0; }//for int sum= pintarray[0];intmax = pintarray[0]; for(inti =1; i < ncount;++i) {if(sum<0){sum=0; }//if sum+ = Pintarray[i];if(Max <sum) {max =sum; }//if}//for returnMax;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
[Huawei Machine Test exercises]56. To find the largest and most sub-arrays