LeetCode 53. Maximum Subarray 最大连续字段和问题

考察:最大连续字段和问题。

解决问题时间复杂度:O(n)

问题隐含条件:如果给出的数集都是负数,那么最大连续字段和就是,最大的那个负数。

eg:{-2,-1}  结果应该输出 -1 而不是 0

int maxSubArray(int* nums, int numsSize) {
    int maxSum = 0;       //维护最大连续字段和
    int currentMaxSum = 0;//当前最大和
    int nextNum = 0;
    int singleSum = nums[0]; //存在全是负数,则singleSum 代表最大的那个
    int j = 0;
    for (int i = 0; i < numsSize; i ++) {
        nextNum = nums[i];
        currentMaxSum += nextNum;
        if (currentMaxSum > 0) {
            maxSum = maxSum <= currentMaxSum  ?  currentMaxSum: maxSum;
        } else {
            currentMaxSum = 0;
            j ++;
            singleSum = singleSum < nextNum ? nextNum : singleSum;
        }
    }
    maxSum = (j == numsSize) ? singleSum : maxSum;
    return maxSum;
}

原文地址:https://www.cnblogs.com/someonelikeyou/p/9611102.html

时间: 2024-10-08 21:04:27

LeetCode 53. Maximum Subarray 最大连续字段和问题的相关文章

41. leetcode 53. Maximum Subarray

53. 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. 思路:这个题还挺经典

LeetCode 53. 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. click to show more practice. Mor

leetCode 53.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. click to show

[LeetCode] 53. 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. 问题: 给定一个元素有正有负的数组,求最大连续子数组的和. 思路

19.2.8 [LeetCode 53] Maximum Subarray

Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum. Example: Input: [-2,1,-3,4,-1,2,1,-5,4], Output: 6 Explanation: [4,-1,2,1] has the largest sum = 6. Follow up: If

leetcode 53 Maximum Subarray ----- java

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. 这道题就是求最大子串和 肯定是o(n)的时间和o(1)的空间 就

Leetcode 53 Maximum Subarray

public class S053 { //kadane's algorithm //最大子数组第一个数肯定大于零,从最大子数组中某个数开始往前的数之和肯定大于零 public int maxSubArray(int[] nums) { if(nums.length == 0) return 0; int sum = 0; int result = Integer.MIN_VALUE; for(int i =0;i<nums.length;i++){ if(sum<0) sum=0; sum+

LeetCode练题——53. Maximum Subarray

1.题目 53. Maximum Subarray——Easy Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum. Example: Input: [-2,1,-3,4,-1,2,1,-5,4], Output: 6 Explanation: [4,-1,2,1] has th

【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. More practice: If you have figu