码迷,mamicode.com
首页 > 编程语言 > 详细

剑指Offer29 连续子数组最大和

时间:2016-09-01 21:28:09      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

 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 }

 

剑指Offer29 连续子数组最大和

标签:

原文地址:http://www.cnblogs.com/Juntaran/p/5831234.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!