1.array
1 package com.example.john.testforgit1; 2 3 /** 4 * Created by john on 2017/3/13. 5 */ 6 7 public class array { 8 9 public int findMaxArray(int[] array){ 10 if (array == null || array.length == 0) { 11 return Integer.MIN_VALUE; 12 } 13 int maxSum = Integer.MIN_VALUE; 14 int currentSum = 0; 15 for (int i = 0; i < array.length; i++) { 16 if (currentSum < 0) { 17 currentSum = array[i]; 18 }else { 19 currentSum += array[i]; 20 } 21 maxSum = Math.max(maxSum, currentSum); 22 } 23 return maxSum; 24 } 25 26 }
2.arrayTest
1 package com.example.john.testforgit1; 2 3 import org.junit.Before; 4 import org.junit.Test; 5 6 import static org.junit.Assert.*; 7 8 /** 9 * Created by john on 2017/3/13. 10 */ 11 public class arrayTest { 12 private array marray;int[] array = {-1,2,3,-4}; 13 @Before 14 public void setUp() throws Exception { 15 marray=new array(); 16 } 17 18 @Test 19 public void findMaxArray() throws Exception { 20 assertEquals(5,marray.findMaxArray(array),0); 21 } 22 23 }
3.测试结果
。