剑指Offer29 连续子数组最大和

 1 /*************************************************************************
 2     > File Name: 29_GreatestSumOfSubArray.c
 3     > Author: Juntaran
 4     > Mail: JuntaranMail@gmail.com
 5     > Created Time: 2016年09月01日 星期四 20时37分22秒
 6  ************************************************************************/
 7 
 8 #include <stdio.h>
 9 
10 int FindGreatestSumOfSubArray(int* nums, int length)
11 {
12     if (nums==NULL || length<=0)
13     {
14         printf("error\n");
15         return -1;
16     }
17     
18     int CurSum = 0;
19     int MaxSum = 0;
20     
21     for (int i = 0; i < length; ++i)
22     {
23         if (CurSum <= 0)
24             CurSum = nums[i];
25         else
26             CurSum += nums[i];
27         
28         if (CurSum > MaxSum)
29             MaxSum = CurSum;
30     }
31     printf("MaxSum is %d\n", MaxSum);
32     return MaxSum;
33 }
34 
35 int main()
36 {
37     int nums[] = {1,-2,3,10,-4,7,2,-5};
38     int length = 8;
39     FindGreatestSumOfSubArray(nums, length);
40 }

 

posted @ 2016-09-01 20:49  Juntaran  阅读(163)  评论(0编辑  收藏  举报