41. 最大子数组
2018-03-06 21:16
155 查看
给定一个整数数组,找到一个具有最大和的子数组,返回其最大和。
样例
给出数组
样例
给出数组
[−2,2,−3,4,−1,2,1,−5,3],符合要求的子数组为
[4,−1,2,1],其最大和为
6
public class Solution { /** * @param nums: A list of integers * @return: A integer indicate the sum of max subarray */ public int maxSubArray(int[] nums) { // write your code here int sum = 0; int max = nums[0]; for (int i = 0; i < nums.length; i++) { sum += nums[i]; if(sum > max){ max = sum; } if (sum < 0) { sum = 0; } } return max; } }
相关文章推荐
- <LeetCode> 题41:最大子数组
- 41 - 最大子数组
- Lintcode 41.最大子数组
- LintCode-41. 最大子数组
- LintCode【简单】41. 最大子数组 。代码及思路
- lintcode 41.最大子数组
- 41.最大子数组
- python_lintcode_简单题 _44最小子数组_41最大子数组
- lintcode--41. 最大子数组
- 数组中最大最小值的蛮力法和二分法求解
- 实验 6 数组1题目1、输入n个整数,将它们存入数组a中。输出最大值和它所对应的下标
- 剑指offer 31 - 连续子数组的最大和
- 子数组求最大和-算法-java
- 面试题31:连续数组的最大和
- HDU 1024 Max Sum Plus Plus(动态规划,给定一个数组,求其分成m个不订交子段和最大值的题目)
- 从字符串数组取得最小时间和最大时间
- 用行指针为函数形参来实现对该二维数组求最大值
- 分治策略之最大子数组问题
- HDU1081 最大字段和 压缩数组
- 数组最大连续和 max sum