leetcode Maximum Subarray(medium) /java

最大和子序列。

在这之前,我这类题目都不是很懂。

今天突然想通了。

最大的问题是如何舍弃不符合的数据。

在if判断中,就能果断丢掉前面的一大串。

import java.io.*;
import java.util.*;

public class Solution {
        public static int maxSubArray(int[] nums) {
        int r;
        int len=nums.length;
        if(len==0)
            return 0;
        int i,j;
        i=0;
        int sum;
        sum=nums[0];
        r=nums[0];
        for(i=1;i<len;i++)
        {
            if(nums[i]>=nums[i]+sum)
                sum=nums[i];
            else
                sum=sum+nums[i];
            if(sum>r)
                r=sum;
        }

        return r;
    }
    public static void main(String[] args)
    {
        int[] a={-2,1,-3,4,-1,2,1,-5,4};
        int[] b={1,-2,2,-3,3,-4};
        System.out.println(maxSubArray(a));
        System.out.println(maxSubArray(b));
    }
}
时间: 2024-11-07 11:30:09

leetcode Maximum Subarray(medium) /java的相关文章

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]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 解题报告

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: 采用滑

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

LEETCODE —— Maximum Subarray [一维DP]

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:

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