[经典算法之数组]
数组中子数组最大和
![[经典算法之数组] 随笔 第1张 [经典算法之数组] 随笔 第1张](https://www.liuyixiang.com/zb_users/theme/Lucky/style/image/grey.gif)
/** * 求数组中最大的子数组之和 动态规划 * * @author ytuan * */ public class MaxSum { public static int begin = 0; public static int end = 0; public static int maxSum(int arr[]) { int n = arr.length; int start = 0; int nMax = 0; int aMax = 0; for (int i = 0; i < n; i++) { if (nMax < 0) { nMax = arr[i]; start = i; } else { nMax += arr[i]; } if (nMax > aMax) { aMax = nMax; begin = start; end = i; } } return aMax; } public static void main(String[] args) { int arr[] = { 1, 2, -5, 6, 8, -5, 9 }; System.out.println(MaxSum.maxSum(arr)); System.out.println(MaxSum.begin); System.out.println(MaxSum.end); } }View Code
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

更多精彩