53:最大子序和
This commit is contained in:
parent
25086a91b3
commit
ed660c8b57
79
src/main/java/leetcode/editor/cn/MaximumSubarray.java
Normal file
79
src/main/java/leetcode/editor/cn/MaximumSubarray.java
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
//给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 1:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:nums = [-2,1,-3,4,-1,2,1,-5,4]
|
||||||
|
//输出:6
|
||||||
|
//解释:连续子数组 [4,-1,2,1] 的和最大,为 6 。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 2:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:nums = [1]
|
||||||
|
//输出:1
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 3:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:nums = [0]
|
||||||
|
//输出:0
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 4:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:nums = [-1]
|
||||||
|
//输出:-1
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 5:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:nums = [-100000]
|
||||||
|
//输出:-100000
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 提示:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 1 <= nums.length <= 3 * 104
|
||||||
|
// -105 <= nums[i] <= 105
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 进阶:如果你已经实现复杂度为 O(n) 的解法,尝试使用更为精妙的 分治法 求解。
|
||||||
|
// Related Topics 数组 分治 动态规划
|
||||||
|
// 👍 3368 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
//53:最大子序和
|
||||||
|
class MaximumSubarray {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new MaximumSubarray().new Solution();
|
||||||
|
}
|
||||||
|
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
public int maxSubArray(int[] nums) {
|
||||||
|
int pre = 0, maxAns = nums[0];
|
||||||
|
for (int x : nums) {
|
||||||
|
pre = Math.max(pre + x, x);
|
||||||
|
maxAns = Math.max(maxAns, pre);
|
||||||
|
}
|
||||||
|
return maxAns;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
53
src/main/java/leetcode/editor/cn/MaximumSubarray.md
Normal file
53
src/main/java/leetcode/editor/cn/MaximumSubarray.md
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
<p>给定一个整数数组 <code>nums</code> ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>nums = [-2,1,-3,4,-1,2,1,-5,4]
|
||||||
|
<strong>输出:</strong>6
|
||||||
|
<strong>解释:</strong>连续子数组 [4,-1,2,1] 的和最大,为 6 。
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>nums = [1]
|
||||||
|
<strong>输出:</strong>1
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 3:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>nums = [0]
|
||||||
|
<strong>输出:</strong>0
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 4:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>nums = [-1]
|
||||||
|
<strong>输出:</strong>-1
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 5:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>nums = [-100000]
|
||||||
|
<strong>输出:</strong>-100000
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>提示:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>1 <= nums.length <= 3 * 10<sup>4</sup></code></li>
|
||||||
|
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>进阶:</strong>如果你已经实现复杂度为 <code>O(n)</code> 的解法,尝试使用更为精妙的 <strong>分治法</strong> 求解。</p>
|
||||||
|
<div><div>Related Topics</div><div><li>数组</li><li>分治</li><li>动态规划</li></div></div>\n<div><li>👍 3368</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user