(int[] nums)
| 1 | public class Solution { |
| 2 | public static int maxSubArr(int[] nums) { |
| 3 | int maxSub = nums[0], |
| 4 | curSum = 0; |
| 5 | for (int i = 0; i < nums.length; i++) { |
| 6 | if (curSum < 0) { |
| 7 | curSum = 0; |
| 8 | } |
| 9 | curSum += nums[i]; |
| 10 | maxSub = Math.max(curSum, maxSub); |
| 11 | } |
| 12 | return maxSub; |
| 13 | } |
| 14 | // just for testing |
| 15 | public static void main(String[] args) { |
| 16 | int[] nums = { -2, 1, -3, 4, -1, 2, 1, -5, 4 }; |