动态规划--求最大连续子串之和
2016-11-15 23:08
211 查看
给定由n个整数(可能为负整数)组成的序列A1,A2,A3,…,An,求该序列的连续子段的和的最大值。
例如 {-4, 11,-2, 13,-7,-3,12} 的最大子段和为24
JAVA代码实现:
例如 {-4, 11,-2, 13,-7,-3,12} 的最大子段和为24
JAVA代码实现:
package com.example; public class 最大连续子串之和 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int[] a = {-4, 11, -2, 13, -7,- 3, 12} ; getMaxSumSeq(a); } private static void getMaxSumSeq(int[] a){ int rmax = Integer.MIN_VALUE; int sum = Integer.MIN_VALUE; int start = -1; int end = -1; int temp = -1; for(int i = 0 ;i<a.length;i++) { if(sum>0) { sum+=a[i]; } else { sum = a[i]; temp = i; } if(sum>rmax) { start = temp; rmax= sum; end = i; } } for(int j = start;j<=end;j++) { System.out.print(a[j]+" "); } System.out.println("\nsum:"+rmax); } }
相关文章推荐
- 动态规划--最大连续子串问题
- 动态规划题目(三)——最大连续乘积子串
- 动态规划方法求最大子连续数组乘积
- 动态规划——最大连续子段和
- 算法竞赛入门经典:第八章 高效算法设计 8.1动态规划之最大连续和
- 动态规划-最大连续乘积子数组
- 动态规划经典题:给出两个字符串s1和s2,返回其中最大的公共子串
- 连续最大子列和的动态规划写法
- 动态规划--求最大连续子数组的和(Python实现)&求解最大连续乘积字串(Python实现)
- 动态规划 最长公共子序列LCS、最长公共连续子串、最长重复子串
- 动态规划——最大连续子段和
- 动态规划--连续子序列的最大和
- 动态规划:给出两个字符串s1和s2,返回其中最大的公共子串
- 动态规划-----最长公共连续子串
- js算法:动态规划-最大公共子串与最大子段和
- 子序列最大连续和(动态规划、分治)
- 最大连续子序列和,乘积,最长递增子串,最长公共子串,子序列等问题(动态规划等)
- 动态规划:最大子串和
- 动态规划典型例题--连续子数组的最大和
- 动态规划求最大连续字序列