最大子序列之和
来源:互联网 发布:gre作文 知乎 编辑:程序博客网 时间:2024/05/05 17:16
问题:给定一个数组,其中至少存在一个不小于零的数,求最大子序列之和全部序列
方法1:比较每个子序列之和,算法复杂度
方法2:比较每个点开始的子序列的大小,算法复杂度
实现代码:
public static void main(String args[]){ private static int[] array = {20, -1, -18, 15, 45, -10, 11}; int thisSum = 0, maxSum = 0, maxFirst = 0, maxLast = 0; for(int i = 0; i < array.length; i++){ thisSum = array[i]; if(thisSum > maxSum){ maxSum = thisSum; maxFirst = maxLast = i; } for(int j = 0; j < array.length; j++){ thisSum += array[j]; if(thisSum > maxSum){ maxSum = thisSum; maxFirst = i; maxLast = j; } } }}
方法3:算法复杂度O(n)
实现代码
public static void main(String args[]){ private static int[] array = {20, -1, -18, 15, 45, -10, 11}; int thisSum = 0, maxSum = 0, maxFirst = 0, maxLast = 0; for(int i = 0; i < array.length; i++){ thisSum += array[i]; if(thisSum > maxSum){ maxSum = thisSum; maxLast = i; } else if(thisSum < 0){ thisSum = 0; maxFirst = i + 1; } }}
0 0
- 最大子序列之和
- 最大子序列之和
- 最大子序列之和
- 最大子序列之和问题
- 求最大子序列之和
- 最大连续子序列之和
- 求最大子序列之和
- 求最大子序列之和
- 最大连续子序列之和
- 最大连续子序列之和
- 最大连续子序列之和
- 最大连续子序列之和
- 最大子连续序列之和
- 最大连续子序列之和
- 最大连续子序列之和,最大连续子序列乘积
- 计算最大连续子序列之和
- 输出最大连续子序列之和
- 最大的子序列之和问题
- WDCP服务器管理系统如何快速上传网站
- C语言大杀器系列 ⑥程序可移植性
- ubuntu 64位安装wps(亲测可用)
- Flux架构学习
- 5-12 两个数的简单计算器
- 最大子序列之和
- java读取/写入属性文件
- C语言大杀器系列 ⑦面向对象
- 传值,传址,引用,ref,out
- 176,xib和storyboard的区别与相同点
- Pascal's Triangle II 输入一个整数,输出该整数对应行数的杨辉三角
- 头文件中的using namespace
- Tomcat安装与配置
- 不用加减法对两数进行相加