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. Delphi各种Socket组件的模式和模型

    Delphi各种Socket组件的模式和模型 Delphi的大多数书籍里面都没有提到delphi的各种socket通信组件的模式和模型,有的书只讲解了windows的socket模式和模型,并没有归纳 ...

  2. MySQL 检索数据及提高检索速度的方法

    检索数据 mysql> SELECT [DISTINCT] 表名.列名,表名.列名,表名.列名 -- 使用通配符*表示所有列 DISTINCT表示返回不同的值 -> FROM 数据库名.表 ...

  3. 自学java坎坷之路——20155312张竞予

    20155312 2006-2007-2 <Java程序设计>第一周学习总结 教材学习内容总结 第一周并没有在课堂上对教材内容进行学习,学习内容概括如下 课程分数构成,其中包括课堂测验(每 ...

  4. centos 7 禁止root登录及更改ssh端口号

    vim /etc/ssh/sshd_config PermitRootLogin yes => PermitRootLogin no systemctl restart sshd.service ...

  5. 深度优先搜索DFS和广度优先搜索BFS

    DFS简介 深度优先搜索,一般会设置一个数组visited记录每个顶点的访问状态,初始状态图中所有顶点均未被访问,从某个未被访问过的顶点开始按照某个原则一直往深处访问,访问的过程中随时更新数组visi ...

  6. nigos core 安装配置

    系统环境      Apache        PHP        GCC compiler        GD development libraries 环境安装     yum install ...

  7. BZOJ 1059 [ZJOI2007]矩阵游戏 (二分图最大匹配)

    1059: [ZJOI2007]矩阵游戏 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 5281  Solved: 2530[Submit][Stat ...

  8. js动态删除某一行,内容超出单元格后超出的部分用省略号代替

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <s ...

  9. C#重点内容之:接口(interface)(一)网络初级示例

    这一篇来源于网络,简单介绍了接口的概念 接口是体现面向对象编程思想优越性的一件利器,为什么这么说呢? 首先我们来看,接口是为继承而存在的,如果没有继承,那就自然不需要接口了,既然有继承,那就需要把可能 ...

  10. 模块import,from ..import...

    首次导入模块发生3件事 1.创建一个模块的名称空间 2.执行文件spam.py,将执行过程中产生的名字都放到模块的名称空间中 3.在当前执行文件中直接拿到一个名字,该名字就是执行模块中相对应的名字 f ...