LeetCode: Maximum Subarray 解题报告

Maximum Subarray

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.

SOLUTION 1:

采用滑动窗口解决。sum 如果小于0,置为0,再加上当前值。

然后再与max相比,取大的。 1分钟AC

 1 public class Solution {
 2     public int maxSubArray(int[] A) {
 3         if (A == null || A.length == 0) {
 4             return 0;
 5         }
 6
 7         int max = Integer.MIN_VALUE;
 8         int sum = 0;
 9
10         int len = A.length;
11         for (int i = 0; i < len; i++) {
12             if (sum < 0) {
13                 sum = 0;
14             }
15
16             sum += A[i];
17             max = Math.max(max, sum);
18         }
19
20         return max;
21     }
22 }

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/array/MaxSubArray_1220_2014.java

时间: 2024-10-03 19:39:09

LeetCode: Maximum Subarray 解题报告的相关文章

[leetcode]Maximum Subarray @ Python

原题地址:https://oj.leetcode.com/problems/maximum-subarray/ 题意: 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,

LeetCode——Maximum Subarray

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. 原题链接: https://oj.leetcode.com/p

LeetCode: Maximum Subarray [052]

[题目] 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. [题意] 给定一个数组,找出和最大的子数组,返回这个

LeetCode: Combination Sum 解题报告

Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Question Solution Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The

[LeetCode]LRU Cache, 解题报告

题目 Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.

【LeetCode】Subsets 解题报告

[题目] Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,3], a solution is: [ [3], [1], [2], [1,2,

leetcode | Maximum Subarray 最大连续子序列的和

Maximum Subarray: https://leetcode.com/problems/maximum-subarray/ 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 [

[LeetCode] Maximum Subarray Sum

Dynamic Programming There is a nice introduction to the DP algorithm in this Wikipedia article. The idea is to maintain a running maximum smax and a current summation sum. When we visit each num in nums, addnum to sum, then update smax if necessary o

leetcode Maximum Subarray 最大子序列

Maximum Subarray Total Accepted: 28381 Total Submissions: 83696 My Submissions 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 contiguo