1
2
3
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.

题意:求连续子数组的最大和

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class Solution {
    public int maxSubArray(int[] nums) {
        int len=nums.length;
        if(nums==null || len==0)return 0;
         int MAX=nums[0];
         int curSum=nums[0];
         for(int i=1;i<len;i++){
             if(curSum>0){
                 curSum+=nums[i];
             }else{
                 curSum=nums[i];
             }
             MAX=Math.max(curSum,MAX);
         }
         return MAX;
         
    }
}

PS:第一种思路,逐渐累加循环。

还有一种思路,可以用动态规划做。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class Solution {
    public int maxSubArray(int[] nums) {
        int len=nums.length;
        if(nums==null || len==0)return 0;
        //dp[i]代表以nums[i]为结尾的最大和。
        int[] dp=new int[len];
        dp[0]=nums[0];
        int MAX=dp[0];
        for(int i=1;i<len;i++){
            if(dp[i-1]<0){
                dp[i]=nums[i];
            }else{
                dp[i]=dp[i-1]+nums[i];
            }
            MAX=Math.max(dp[i],MAX);
        }
        return MAX;
    }
}

本文转自 努力的C 51CTO博客,原文链接:http://blog.51cto.com/fulin0532/1905435

Leetcode 53. Maximum SubarrayJAVA语言相关推荐

  1. 【DP】LeetCode 53. Maximum Subarray

    LeetCode 53. Maximum Subarray Solution1:我的答案 动态规划 class Solution { public:int maxSubArray(vector< ...

  2. 【动态规划】LeetCode 53. Maximum Subarray

    LeetCode 53. Maximum Subarray 原题描述(求子序列最大和/最大子串):Find the contiguous subarray within an array (conta ...

  3. [Lintcode]41. Maximum Subarray/[Leetcode]53. Maximum Subarray

    41. Maximum Subarray/53. Maximum Subarray 本题难度: Eas Topic: Dynamic Programming Description Given an ...

  4. LeetCode 53. Maximum Subarray--动态规划--C++,Python解法

    题目地址:Maximum Subarray - LeetCode Given an integer array nums, find the contiguous subarray (containi ...

  5. LeetCode(53):Maximum Subarray

    Maximum Subarray: Find the contiguous subarray within an array (containing at least one number) whic ...

  6. [LeetCode]: 53: Maximum Subarray

    题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...

  7. leetCode 53. maximum subarray

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  8. LeetCode#53 Maximum Subarray

    Problem Difinition: Find the contiguous subarray within an array (containing at least one number) wh ...

  9. C#解leetcode 53.Maximum Subarray

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

最新文章

  1. document.all
  2. optaplanner_OptaPlanner –具有真实道路距离的车辆路线
  3. qdir 类似工具_qdir 类似工具_支持 Win8.1,全能资源管理器 Q-Dir 5.74 发布
  4. 千兆网线8根线定义图_网线水晶头如何制作及怎么测试?
  5. [脚本收集]提取Tripntale图片
  6. 【渝粤教育】电大中专成本会计作业 题库
  7. 树莓派使用usb摄像头
  8. 使用腾讯云服务器搭建自己网站应该怎么做
  9. android组合按键截屏,如果你的安卓手机支持〔电源〕键+〔音量减〕键截屏,你可以代码...
  10. 天才小毒妃 第943章 小东西很努力
  11. 对比工具winMerge
  12. 一位博士在华为的 22 年
  13. 利用身份证号码算年龄 并排序
  14. Android-Universal-Image-Loader三大组件DisplayImageOptions、ImageLoader、ImageLoaderConfiguration详解
  15. sourceTree使用教程
  16. DotNetTextBox V3.0 所见即所得编辑器控件Ver3.2.4 Free(免费版)
  17. netty-Android-RN等博文收藏
  18. Unity学习笔记(实现传送带)
  19. HTB walkthrough -- Admirer
  20. 迅雷下载的资源,文件夹有大小,但是打开没有文件

热门文章

  1. 苹果6发布时间_iPhone12promax11月6日几点预售 11.6苹果12mini预售时间
  2. 剑指offer面试题[14]-调整数组顺序使奇数位于偶数前面
  3. 理解这几个安全漏洞,你也能做安全测试【干货建议收藏】
  4. Python操作数据库完成接口测试
  5. linux mysql 实战_Linux平台MySQL多实例项目实施_MySQL数据库基础与项目实战06
  6. 你的手机浏览器不支持webgle_不支持n79频段5G手机不能买?OPPO Reno3全频覆盖消除后顾之忧...
  7. undo the local changes
  8. papers to read
  9. Hierarchical deformation of Locally Rigid Meshes
  10. Lesson 2 Gradient Desent