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.

题目大意:给定一个数组,求最大连续子数组和。

解题思路:DP问题,首先分析一下数组,有正有负,那么可以假设第i个元素对应的最大连续子数组和是F[i],那么写出递推公式

F[i]=max{F[i-1]+num[i],num[i]}

就是说第i个元素最大连续子数组和是前i-1个最大连续子数组加上自己与自己相比,较大的那个;如果F[i-1]<0,那么F[i]=num[i],否则F[i]=F[i-1]+num[i],同时记录一个全局的最大值来更新。

    public int maxSubArray(int[] nums) {
if(nums==null||nums.length==0){
return 0;
}
int max = nums[0];
int res = nums[0];
for(int i=1;i<nums.length;i++){
max=Math.max(nums[i]+max,nums[i]);
res = Math.max(res,max);
}
return res;
}

Maximum Subarray——LeetCode的更多相关文章

  1. Maximum Subarray - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Maximum Subarray - LeetCode 注意点 最大值有可能是正负数交替着出现 解法 解法一:一次遍历即可.当sum小于0的时候就重新开始 ...

  2. Maximum Subarray leetcode java

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

  3. [Leetcode][Python]53: Maximum Subarray

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 53: Maximum Subarrayhttps://leetcode.co ...

  4. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  5. LeetCode 53. Maximum Subarray(最大的子数组)

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

  6. [array] leetcode - 53. Maximum Subarray - Easy

    leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...

  7. 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略

    原题 Given an integer array nums, find the contiguous subarray (containing at least one number) which ...

  8. LeetCode 53. 最大子序和(Maximum Subarray)

    53. 最大子序和 53. Maximum Subarray 题目描述 给定一个整数数组 nums,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. LeetCode53. M ...

  9. Leetcode之53. Maximum Subarray Easy

    Leetcode 53 Maximum Subarray Easyhttps://leetcode.com/problems/maximum-subarray/Given an integer arr ...

随机推荐

  1. codevs 1519 过路费 最小生成树+倍增

    /*codevs 1519 过路费 最小生成树+倍增*/ #include<iostream> #include<cstdio> #include<cstring> ...

  2. 浅淡Webservice、WSDL三种服务访问的方式(附案例)

    Webservice Webservice是使应用程序以与平台和编程语言无关的方式进行相互通信技术. eg:站点提供访问的数据接口:新浪微博.淘宝. 官方解释:它是一种构建应用程序的普遍模型,可以在任 ...

  3. acl操作记录

    官方文档内容: 1.CREATE_ACL Procedure创建ACL Note: This procedure is deprecated in Oracle Database 12c. While ...

  4. 一个Socket数据处理模型

    Socket编程中,如何高效地接收和处理数据,这里介绍一个简单的编程模型. Socket索引 - SocketId 在给出编程模型之前,先提这样一个问题,程序中如何描述Socket连接? 为什么这么问 ...

  5. Sublime Text 3运行JavaScript控制台

    Node.js是一个基于Chrome JavaScript运行时建立的平台,小巧方便搭建.运行的端口可以在浏览器上运行,显示效果,但每次用浏览器也挺麻烦,我们这里讲的是在sublime text2中配 ...

  6. Codeforces 193D Two Segments 解题报告

    先是在蓝桥杯的网站上看到一道题: 给出1~n的一个排列,求出区间内所有数是连续自然数的区间的个数.n<=50000. 由于数据较弱,即使用O(N^2)的算法也能拿到满分. 于是在CF上发现了这一 ...

  7. Java jdk环境搭建

    java JDK的配置在我的电脑环境变量中配置: 主要的配置参数path  例:path = C:\jdk1.7.0_13\bin ; 另外一个JAVA_HOME 例:JAVA_HOME = C:\j ...

  8. JavaScript 获取Select标签选中的项

    <select name="select1" id="select1" onchange=setInput()> <option value= ...

  9. 再次深入理解delphi的类

    property WindowState: TWindowState read FWindowState write SetWindowState; {声明一个属性WindowState,它从字段FW ...

  10. WAMP集成环境更改web根目录

    使用WAMP集成环境,如何更改web根目录 做php开发使用WAMP集成环境的同学大部分有过这样的经历:如果你试图修改web根目录,那么你肯定会想到要修改apache/apache2.2.11/con ...