剑指 Offer 42:连续子数组的最大和
This commit is contained in:
parent
53a8669da9
commit
0347cbcb08
src/main/java/leetcode/editor/cn
@ -0,0 +1,57 @@
|
||||
//输入一个整型数组,数组中的一个或连续多个整数组成一个子数组。求所有子数组的和的最大值。
|
||||
//
|
||||
// 要求时间复杂度为O(n)。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例1:
|
||||
//
|
||||
// 输入: nums = [-2,1,-3,4,-1,2,1,-5,4]
|
||||
//输出: 6
|
||||
//解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// 1 <= arr.length <= 10^5
|
||||
// -100 <= arr[i] <= 100
|
||||
//
|
||||
//
|
||||
// 注意:本题与主站 53 题相同:https://leetcode-cn.com/problems/maximum-subarray/
|
||||
//
|
||||
//
|
||||
// Related Topics 数组 分治 动态规划
|
||||
// 👍 339 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import java.util.Stack;
|
||||
|
||||
//剑指 Offer 42:连续子数组的最大和
|
||||
class LianXuZiShuZuDeZuiDaHeLcof {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new LianXuZiShuZuDeZuiDaHeLcof().new Solution();
|
||||
System.out.println(solution.maxSubArray(new int[]{-1}));
|
||||
System.out.println(solution.maxSubArray(new int[]{-2, 1, -3, 4, -1, 2, 1, -5, 4}));
|
||||
System.out.println(solution.maxSubArray(new int[]{-2, -1}));
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int maxSubArray(int[] nums) {
|
||||
int cur = 0;
|
||||
int max = Integer.MIN_VALUE;
|
||||
for (int num : nums) {
|
||||
cur = Math.max(cur + num, num);
|
||||
max = Math.max(max, cur);
|
||||
}
|
||||
return max;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
<p>输入一个整型数组,数组中的一个或连续多个整数组成一个子数组。求所有子数组的和的最大值。</p>
|
||||
|
||||
<p>要求时间复杂度为O(n)。</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> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= arr.length <= 10^5</code></li>
|
||||
<li><code>-100 <= arr[i] <= 100</code></li>
|
||||
</ul>
|
||||
|
||||
<p>注意:本题与主站 53 题相同:<a href="https://leetcode-cn.com/problems/maximum-subarray/">https://leetcode-cn.com/problems/maximum-subarray/</a></p>
|
||||
|
||||
<p> </p>
|
||||
<div><div>Related Topics</div><div><li>数组</li><li>分治</li><li>动态规划</li></div></div>\n<div><li>👍 339</li><li>👎 0</li></div>
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user