letcode code]Maximum Subarray
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.
maxSum = Math.max(maxSum, curSum);
改为if语句,存开始序号和子串长度。
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的更多相关文章
- maximum subarray problem
In computer science, the maximum subarray problem is the task of finding the contiguous subarray wit ...
- Maximum Subarray Sum
Maximum Subarray Sum 题意 给你一个大小为N的数组和另外一个整数M.你的目标是找到每个子数组的和对M取余数的最大值.子数组是指原数组的任意连续元素的子集. 分析 参考 求出前缀和, ...
- [array] leetcode - 53. Maximum Subarray - Easy
leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...
- 53. Maximum Subarray最大求和子数组12 3(dp)
[抄题]: Find the contiguous subarray within an array (containing at least one number) which has the la ...
- [LeetCode] Maximum Subarray Sum
Dynamic Programming There is a nice introduction to the DP algorithm in this Wikipedia article. The ...
- [LintCode] Maximum Subarray 最大子数组
Given an array of integers, find a contiguous subarray which has the largest sum. Notice The subarra ...
- 【leetcode】Maximum Subarray (53)
1. Maximum Subarray (#53) Find the contiguous subarray within an array (containing at least one nu ...
- 算法:寻找maximum subarray
<算法导论>一书中演示分治算法的第二个例子,第一个例子是递归排序,较为简单.寻找maximum subarray稍微复杂点. 题目是这样的:给定序列x = [1, -4, 4, 4, 5, ...
- LEETCODE —— Maximum Subarray [一维DP]
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
随机推荐
- HDU 5988.Coding Contest 最小费用最大流
Coding Contest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- Eclipse中配置Tomcat服务器并创建标准Web目录
Eclipse创建 Java Web 项目,并生成标准的目录结构 file --> New --> Dynamic Web project 填写 Project name (该名称项目的名 ...
- nginx 域名(虚拟)部署nodejs项目
首先说下我的情况,Windows+mongodb开发的简单nodejs 小博客系统, 配置部署到centos7 nginx下,mongodb还是在我Windows机器下, 1.Linux安装node. ...
- ajax post 请求 ,java端使用 request.getParameter 获取不到数据问题
js端 $.ajax({ type:'POST', data:{a:1}, url:_this.apiUrl+url, dataType:'json',//使用jsonp方式请求 contentTyp ...
- php实现MySQL两库对比升级版
define('DATABASE1', 'db1'); $dbi1 = new DbMysql; $dbi1->dbh = 'mysql://root:password@127.0.0.1/'. ...
- 金币(NOIP2015)
先给题目:金币 又是很水的题,很简单,直接上代码: #include<bits/stdc++.h> using namespace std; int main(){ int n; scan ...
- HTML and CSS学习概述-续
1, CSS是层叠样式表(Cascading Style Sheets)的缩写,它用于定义HTML元素的显示形式,是一种格式化网页内容的技术.CSS现在已经被大多数浏览器所支持,成为网页设计者必须 ...
- LD_LIBRARY_PATH
LD_LIBRARY_PATH是Linux环境变量名,该环境变量主要用于指定查找共享库(动态链接库)时除了默认路径之外的其他路径. 在linux下可以用export命令来设置这个值,比如 在linux ...
- AOP (切点表达式讲解)
Spring EL表达式:: 1.execution 表达式 语法格式: execution(返回类型.包名.类名.方法名(参数表)) exection(*.com.xxx.AService.*(.. ...
- java笔记--问题总结
1. 垃圾回收算法 标记-清除算法 标记-清除算法是最基本的算法,和他的名字一样,分为两个步骤,一个步骤是标记需要回收的对象.在标记完成后统一回收被标记的对象.这个算法两个问题.一个是效率问题,标记和 ...