首页 > 其他 > 详细

Maximum Subarray

时间:2015-01-18 11:50:51      阅读:249      评论:0      收藏:0      [点我收藏+]

题目:

Find the contiguous subarray within an array (containing at least one number) which has the largest sum.

For example, given the array [?2,1,?3,4,?1,2,1,?5,4],
the contiguous subarray [4,?1,2,1] has the largest sum = 6.

我的解法:

(1)算法思想:

记sum=max=A[0],顺序遍历一次数组,将A[i]加到sum中,如果sum小于A[i],则用A[i]替换sum;如果sum大于max,则用sum替换max。遍历完,返回max即可。

(2)代码如下:


Maximum Subarray

原文:http://blog.csdn.net/littlebob180/article/details/42834211

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!