Maximum Subarray:数组中最大连续区间和
来源:互联网 发布:网络新技术论文 编辑:程序博客网 时间:2024/05/29 15:39
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.
For example, given the array [-2,1,-3,4,-1,2,1,-5,4]
,
the contiguous subarray [4,-1,2,1]
has the largest sum = 6
.
click to show more practice.
More practice:
If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
思路:去年算法课的一道作业题,连续求一段和,若大于零则保留,小于零则抛弃,重新累加求和,记录下中间过程中的最大者即为答案。
class Solution { public int maxSubArray(int[] nums) { int sum = 0; int ans = nums[0]; for(int i =0;i<nums.length;i++){ sum+=nums[i]; ans = Math.max(ans,sum); sum = Math.max(0,sum); } return ans; }}
阅读全文
0 0
- Maximum Subarray:数组中最大连续区间和
- Maximum Product Subarray:数组中最大连续区间求积
- Maximum Subarray-最大连续子数组和
- Maximum Subarray 最大连续和
- Maximum Subarray 最大子数组和(连续)@LeetCode
- 53.Maximum Subarray&连续子数组的最大和
- leetcode-53 Maximum Subarray 连续子数组的最大和
- LeetCode OJ-53.Maximum Subarray(最大连续子数组和)
- [LeetCode] Maximum Subarray 求连续子数组的最大和
- LeetCode 53. Maximum Subarray--连续子数组的最大和
- Maximum Subarray 最大连续子数和
- LeetCode-53:Maximum Subarray (和最大的连续子数组)
- LeetCode | Maximum Subarray(连续最大子数组)
- Maximum Product Subarray 连续数组的最大乘积
- 【LeetCode】152. Maximum Product Subarray最大连续子数组乘积
- Array-----152. Maximum Product Subarray(最大连续子数组乘积)
- LeetCode 152. Maximum Product Subarray--连续子数组最大乘积
- 最大子数组和 (Maximum Subarray)
- poj2409 Let it Bead(置换)
- 【BZOJ4282】慎二的随机数列(LIS)
- 自定义圆及箭头加减速
- Python 之 高级面向对象编程 slots
- HDU 1517 A Multiplication Game 博弈论+找规律
- Maximum Subarray:数组中最大连续区间和
- 【BZOJ1880】【SDOI2009】Elaxia的路线
- [BZOJ1880][Sdoi2009] Elaxia的路线 (最短路,拓扑排序)
- qbxt Day 1 乱搞+乱搞+DP
- 判断网络状态
- JavaScript温故知新----Array.sort()
- 2017.10.9 DZY Loves Math VI 失败总结
- 【BZOJ3714】【PA2014】Kuglarz
- css各种动画