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. The performance between the 'normal' operation and the 'shift' operation.

    First, I gonna post my test result with some code: //test the peformance of the <normal operation ...

  2. VS 编程常见错误及解决方法

    1. VS2013 无法打开包括文件:“cv.h"等一些头文件 解决方法: cv.h是build\include文件夹下的头文件,所在文件夹位置是D:\Program Files (x86) ...

  3. 【POJ2887】【块状链表】Big String

    Description You are given a string and supposed to do some string manipulations. Input The first lin ...

  4. wordpress整站搬家总结

    去年图便宜,也没准备认真写博文,所以花了几百元钱买了个国内空间(域名已经备案).购买了以后,放了一个wordpress博客,没事的时候写写博文,但从没有抽出时间去写,文章的质量也不追求.一开始还可以, ...

  5. dedecms设置文章分页后,标题会带有序号的解决方法

    至于删除分页后标题后面的序号,找到include/arc.archives.class.php 打开,找到 if($i>1) $this->Fields['title'] = $this- ...

  6. 浏览器JS报错Uncaught RangeError: Maximum call stack size exceeded?

    JavaScript错误:Uncaught RangeError: Maximum call stack size exceeded 堆栈溢出 原因:有小类到大类的递归查询导致溢出 解决方法思想: A ...

  7. TCP协议握手与分手

    TCP(Transmission Control Protocol) 传输控制协议 TCP的7次握手可以理解为3次握手和4次分手. TCP状态转换图,如下: 这个图N多人都知道,它对排除和定位网络或系 ...

  8. Python自动化运维之22、JavaScript

    一.简介 JavaScript是一门编程语言,浏览器内置了JavaScript语言的解释器,所以在浏览器上按照JavaScript语言的规则编写相应代码之,浏览器可以解释并做出相应的处理.学习了htm ...

  9. UFLDL实验报告3:Self-taught

    Self-taught 自我学习器实验报告 1.Self-taught 自我学习实验描述 自我学习是无监督特征学习算法,自我学习意味着算法能够从未标注数据中学习,从而使机器学习算法能够获得更大数量的数 ...

  10. 转:Windows下的PHP开发环境搭建——PHP线程安全与非线程安全、Apache版本选择,及详解五种运行模式。

    原文来自于:http://www.ituring.com.cn/article/128439 Windows下的PHP开发环境搭建——PHP线程安全与非线程安全.Apache版本选择,及详解五种运行模 ...