53. Maximum Subarray

Easy

Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.

Example:

Input: [-2,1,-3,4,-1,2,1,-5,4],
Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6.

Follow up:

If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.

package leetcode.easy;

public class MaximumSubarray {
@org.junit.Test
public void test() {
int[] nums = { -2, 1, -3, 4, -1, 2, 1, -5, 4 };
System.out.println(maxSubArray(nums));
} public int maxSubArray(int[] nums) {
int max = nums[0];
int count = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] > max) {
max = nums[i];
}
count = nums[i];
for (int j = i + 1; j < nums.length; j++) {
count += nums[j];
if (count > max) {
max = count;
}
}
}
return max;
}
}

LeetCode_53. Maximum Subarray的更多相关文章

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

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

  2. 【leetcode】Maximum Subarray (53)

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

  3. 算法:寻找maximum subarray

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

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

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

  5. 【leetcode】Maximum Subarray

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

  6. maximum subarray problem

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

  7. (转)Maximum subarray problem--Kadane’s Algorithm

    转自:http://kartikkukreja.wordpress.com/2013/06/17/kadanes-algorithm/ 本来打算自己写的,后来看到上述链接的博客已经说得很清楚了,就不重 ...

  8. 3月7日 Maximum Subarray

    间隔2天,继续开始写LeetCodeOj. 原题: Maximum Subarray 其实这题很早就看了,也知道怎么做,在<编程珠玑>中有提到,求最大连续子序列,其实只需要O(n)的复杂度 ...

  9. LeetCode: Maximum Product Subarray && Maximum Subarray &子序列相关

    Maximum Product Subarray Title: Find the contiguous subarray within an array (containing at least on ...

随机推荐

  1. 《hello--world团队》第五次作业:项目需求分析改进与系统设计

    项目 内容 这个作业属于哪个课程 2016级计算机科学与工程学院软件工程(西北师范大学) 这个作业的要求在哪里 实验九 团队作业5-团队项目需求改进与系统设计 团队名称 <hello--worl ...

  2. git致命错误汇总

    1.  拒绝合并无关历史 fatal: refusing to merge unrelated histories 解决办法: git pull origin master --allow-unrel ...

  3. 块状链表 codevs 2333弹飞绵羊

    块状链表,分块处理,先预处理每一个点跳到下一个块 跳到哪,步数.然后修改的时候,修该那一个块即可 #include<cstdio>#include<cmath>int a[20 ...

  4. git命令如何删除文件或文件夹

    拉取远程仓到本地 git clone ×× cd ××× 查看分支 git branch -a 切换到想要操作的分支 git checkout 想要操作的分支 在本地仓库删除文件 git rm 我的文 ...

  5. 2019暑期金华集训 Day5 生成函数

    自闭集训 Day5 生成函数 一般生成函数 无脑地把序列变成多项式: \[ \{a_i\}\rightarrow A(x)=\sum_{n} a_nx^n \] 形式幂级数 生成函数是一种形式幂级数. ...

  6. Python3循环

    Python中while语句的一般形式: while 判断条件: 语句 同样需要注意冒号和缩进,另外在Python中没有do…while循环 下面的实例计算1到100总和 ##calc.py n = ...

  7. 【模板】杜教筛(Sum)

    传送门 Description 给定一个正整数\(N(N\le2^{31}-1)\) 求 \[ans1=\sum_{i=1}^n \varphi(i)\] \[ans_2=\sum_{i=1}^n \ ...

  8. Android Studio如何删除一个Module

    当你想在Android Studio中删除某个module时,大家习惯性的做法都是选中要删除的module,右键去找delete.但是在Android  Studio中你选中module,右键会发现没 ...

  9. [Shell]Powershell反弹shell

    原作者:Cream 文章出处: 贝塔安全实验室 0x01 Powershell反弹shell Windows PowerShell 是一种命令行外壳程序和脚本环境,使命令行用户和脚本编写者可以利用 . ...

  10. 说出Servlet的生命周期,并说出Servlet和CGI的区别。

    说出Servlet的生命周期,并说出Servlet和CGI的区别. 山治ZHrx5 | 浏览 1377 次 推荐于2016-09-16 22:39:19 最佳答案 Servlet的生命周期分为5个阶段 ...