[Huawei Machine Test exercises]56. To find the largest and most sub-arrays

Source: Internet
Author: User

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

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.