53. Maximum Subarray【leetcode】

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.

挑选子串中最大的值,自己写了一堆代码最后缺少负数情况的考虑,

public class Solution {
    public int maxSubArray(int[] nums) {
        //第一次找一个数组 记录所有大于0的数字的位置
        //结果=第一个非负数
        //逐次循环加到下一个非负数,如果比当前结果大,则替换当前结果
        int resMax=nums[0];
        int res =nums[0] ;
        int len = nums.length;
        int vaNums[] = new int [len];
        int j=0;
       // Map<Integer,Integer> map =new HaspMap<Integer,Integer>();
        int m=0;
        int plusNums[] = new int [len];
        for(int i=0;i<len;i++){
            if(nums[i]>0){
                vaNums[j]=i;
               // map.put(i,nums[i]);
                j++;
            }
            else{
                plusNums[m]=i;
                 m++;
            }
            res+=nums[i];
        }
        if(j>0){

            for(int k=0;k<j;k++){
                res =0;
                for(int l =vaNums[k];l<=vaNums[j-1];l++){
                    res+=nums[l];
                    if(resMax<res){
                        resMax=res;
                    }
                }

            }
        }//if j >0 end
        else {
            for(int k=0;k<m;k++){
                res =nums[0];
                for(int l =vaNums[k];l<plusNums[m-1];l++){
                    res+=nums[l];
                    if(resMax<res){
                        resMax=res;
                    }
                }

            }
        }

        return resMax;
    }
}

最佳办法,感觉是真滴牛逼

public class Solution {
    public int maxSubArray(int[] nums) {
        //第一次找一个数组 记录所有大于0的数字的位置
        //结果=第一个非负数
        //逐次循环加到下一个非负数,如果比当前结果大,则替换当前结果
        int sum=0,max=Integer.MIN_VALUE;
        int len =nums.length;
        for(int i=0;i<len;i++){
            sum +=nums[i];
            max =Math.max(sum,max);
            sum = Math.max(0,sum);
        }
//方法二

      /*
        int sum=0,max=Integer.MIN_VALUE,minSum = 0;
        for (int i = 0; i < nums.length; i++) {
            sum += nums[i];
            max = Math.max(max, sum - minSum);
            minSum = Math.min(minSum, sum);
        }
    */

        return max;
    }
}
时间: 2024-11-06 16:43:32

53. Maximum Subarray【leetcode】的相关文章

【LeetCode】数组

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [1]Two Sum [4]Median of Two Sorted Arrays [11]Container With Most Water [15]3Sum [16]3Sum Closest [18]4Sum [26]Remove Duplicates from Sorted Array [27]Remove Element [31]Next Permutatio

【LeetCode】动态规划(上篇共75题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [5] Longest Palindromic Substring 给一个字符串,需要返回最长回文子串 解法:dp[i][j] 表示 s[i..j] 是否是回文串,转移方程是 dp[i][j] = 1 (if dp[i+1][j-1] = 1 && s[i] == s[j]),初始化条件是 if (s[i] == s[j] && (i == j

【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

【LeetCode】 Maximum Depth of Binary Tree

Maximum Depth of Binary Tree  Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 递归基础,不解释. class Solution { public: int getMax(TreeNode *root

【leetcode】998. Maximum Binary Tree II

题目如下: We are given the root node of a maximum tree: a tree where every node has a value greater than any other value in its subtree. Just as in the previous problem, the given tree was constructed from an list A (root = Construct(A)) recursively with

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

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】动态规划(下篇共39题)

[600] Non-negative Integers without Consecutive Ones [629] K Inverse Pairs Array [638] Shopping Offers [639] Decode Ways II [646] Maximum Length of Pair Chain [647] Palindromic Substrings [650] 2 Keys Keyboard [651] 4 Keys Keyboard [656] Coin Path [6

【LeetCode】Jump Game

Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. For example:A =