LeetCode题解:Maximum Subarray
来源:互联网 发布:java style标签 编辑:程序博客网 时间:2024/06/05 10:42
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.
题意:找出数组中和最大的子数组
解题思路:DP
代码:
public class Solution { public int maxSubArray(int[] nums) { int maxSoFar=nums[0]; int maxEndingHere=nums[0]; for (int i = 1; i < nums.length; ++i){ maxEndingHere = Math.max(maxEndingHere + nums[i], nums[i]); maxSoFar = Math.max(maxSoFar, maxEndingHere); } return maxSoFar; }}
0 0
- LeetCode题解:Maximum Subarray
- LeetCode题解:Maximum Subarray
- LeetCode题解:Maximum Subarray
- [LeetCode]Maximum Subarray题解
- LeetCode题解:Maximum Product Subarray
- LeetCode 题解(172): Maximum Subarray
- LeetCode 53. Maximum Subarray 题解
- 【Leetcode题解】53. Maximum Subarray
- LeetCode 题解(57): Maximum Product Subarray
- LeetCode 练习题解(152. Maximum Product Subarray)
- leetcode题解——53. Maximum Subarray
- Leetcode题解 53. Maximum Subarray 思路解析
- LeetCode53. Maximum Subarray题解
- LeetCode题解系列--718. Maximum Length of Repeated Subarray
- 【LeetCode】Maximum Subarray 和 Maximum Product Subarray
- LeetCode: Maximum Subarray
- LeetCode Maximum Subarray
- [Leetcode] Maximum Subarray
- c++知识点
- 递归求幂
- 平衡二叉树
- linux常用命令
- Linux apache 添加 mod_rewrite模块
- LeetCode题解:Maximum Subarray
- iOS - App内使用代码退出程序
- html5兼容IE placeholder问题
- 昨天临摹了网店的首页。以后可以自己装修用所以格外用心了些
- 进程间通信
- MacOS安装与运行MongoDB
- Linux下查看文件内容的命令
- JAVA SDutOJ 题目收录
- 软件测试(一) 近期的一次debug的经历