笔试题---最大子序列和

 1 public class Solution {
 2      public int FindGreatestSumOfSubArray(int[] array) {
 3          if (array.length==0 || array==null) {
 4              return 0;
 5          }
 6          int curSum=0;
 7          int greatestSum=0x80000000;
 8          for (int i = 0; i < array.length; i++) {
 9              if (curSum<=0) {
10                  curSum=array[i]; //记录当前最大值
11              }else {
12                  curSum+=array[i]; //当array[i]为正数时,加上之前的最大值并更新最大值。
13              }
14              if (curSum>greatestSum) {
15                  greatestSum=curSum; 
16              }
17          }
18          return greatestSum;
19      }
20  }

 

posted @ 2015-09-26 01:40  阿司★*^_^*联波  阅读(159)  评论(0编辑  收藏  举报