package com.promise.demo; public class MaxSubSum { // DP法求解 最大子串和 public static int maxSubSum(int[] array){ if(array == null) { return 0; } int n = array.length; int[] end = new int[n]; int[] res = new int[n]; end[0] = array[0]; res[0] = array[0]; for(int i=1;i<n;i++){ end[i] = Integer.max(array[i],end[i-1]+array[i]); res[i] =Integer.max(end[i],res[i-1]); } return res[n-1]; } public static void main(String[] args){ int[] array = {1,-2,4,8,-4,7,-1,-5}; int max = maxSubSum(array); System.out.println(max); } }
机试【二】求连续的最长子串和
最新推荐文章于 2024-04-19 10:18:00 发布