1 题目:

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.

click to show more practice.

Hide Tags

Divide and Conquer Array Dynamic Programming

 
2 分析:
要找最大连续的子数组。好吧,动态规划题,想了半天,想不出递推式怎么写,可能与做的太少有关。只分析出求F(n)的话,有两种情况,一种是F(n-1)最大子串里面有A[n-1],一种没有,然后我就不知道怎么写了。。。看了看别人的代码,分析了一下,定义一个curSum,取肯定有A[i]的最大子串,然后比较maxSum与curSum就行了。
标记函数可以将
 maxSum = Math.max(maxSum, curSum);
改为if语句,存开始序号和子串长度。
 
3 代码:
    public int maxSubArray(int[] A){
if (A.length == 0) {
return 0;
}
int maxSum = A[0];
int curSum = A[0];
int len = A.length;
for (int i = 1; i < len; i++) {
curSum = Math.max(curSum + A[i], A[i]);
maxSum = Math.max(maxSum, curSum);
}
return maxSum;
}

letcode code]Maximum Subarray的更多相关文章

  1. maximum subarray problem

    In computer science, the maximum subarray problem is the task of finding the contiguous subarray wit ...

  2. Maximum Subarray Sum

    Maximum Subarray Sum 题意 给你一个大小为N的数组和另外一个整数M.你的目标是找到每个子数组的和对M取余数的最大值.子数组是指原数组的任意连续元素的子集. 分析 参考 求出前缀和, ...

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

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

  4. 53. Maximum Subarray最大求和子数组12 3(dp)

    [抄题]: Find the contiguous subarray within an array (containing at least one number) which has the la ...

  5. [LeetCode] Maximum Subarray Sum

    Dynamic Programming There is a nice introduction to the DP algorithm in this Wikipedia article. The ...

  6. [LintCode] Maximum Subarray 最大子数组

    Given an array of integers, find a contiguous subarray which has the largest sum. Notice The subarra ...

  7. 【leetcode】Maximum Subarray (53)

    1.   Maximum Subarray (#53) Find the contiguous subarray within an array (containing at least one nu ...

  8. 算法:寻找maximum subarray

    <算法导论>一书中演示分治算法的第二个例子,第一个例子是递归排序,较为简单.寻找maximum subarray稍微复杂点. 题目是这样的:给定序列x = [1, -4, 4, 4, 5, ...

  9. LEETCODE —— Maximum Subarray [一维DP]

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

随机推荐

  1. dede 栏目及子栏目

    结果如图: <ul class="bottom_ul"> {dede:channelartlist } <li><a href="{dede ...

  2. Python GUI中 text框里实时输出

    首先GUI中不同函数的局部变量的问题. 发现不同button定义的函数得到的变量无法通用. 通过global 函数内的变量可以解决这个问题 def openfiles2(): global s2fna ...

  3. include 指令和 include 动作引入 jsp 页面时中文乱码

    include指令:<%@ include file="new.jsp" %> include动作:<jsp:include page="new.jsp ...

  4. Spring Environment(一)API 介绍

    Spring Environment(一)API 使用 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) Spring 3. ...

  5. 第一个spring boot 程序

    安装.运行.预览省略 错误1:8080端口被IIS占用,关闭它 Description: The Tomcat connector configured to listen on port 8080 ...

  6. mysql链接

    一 Mysql命令行连接 一般对于刚刚安装好的mysql,如果勾选启用mysql lineclient的话.可以直接通过找到开始---程序--- mysql command line client如下 ...

  7. tolua杂记

    1 字符串调用luaFunc  :DoString public class CallLuaFunction : MonoBehaviour { private string script = @&q ...

  8. Spring中@Autowired注解、@Resource注解的区别 (zz)

    Spring中@Autowired注解.@Resource注解的区别 Spring不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource.@ ...

  9. gunicorn配置文件

    最近使用gunicorn部署,感觉用命令参数方式启动比较繁琐,而且有时候就忘了以前怎么设置的了.一笑... 上stackoverflow查了查,找到了一个官方示例,在这里. 官方解释在这里. 记在这里 ...

  10. Devexpress VCL Build v2015 vol 15.1.2发布

    2015年马上过半年了.终于第一个大版出来了. What's New in 15.1.2 (VCL Product Line)   New Major Features in 15.1 What's ...