Leetcode_53_Maximum Subarray
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/43989997
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.
思路:
(1)题意为给定整数数组,求解数组中连续子数组之和的最大值。
(2)这是一道比较经典的笔试面试题。主要考查对数组的运用。由于数组中的元素可能为正,也可能为负,所以,要得到连续元素的最大值,需对数组遍历过程中出现负值时进行判断。这样,只需遍历数组一次(初始化当前连续序列之和sum=0,最大值max=x[0]),在遍历的过程中,如果当前sum>=0,说明连续序列之和为正,将当前遍历元素的数值加到sum中;如果sum<0,说明在之前遍历过程中遇到了负数,将当前遍历元素的数值赋给sum;如果sum比当前最大值max要大,则将sum的值赋给max;遍历完数组后,max即为所求。
(3)该题主要需考虑正负数交替的情况以及全是负数的情况,详情参见下方代码。希望本文对你有所帮助。
算法代码实现如下:
/**
* @author liqq
*/
public class Maximum_Subarray{
public int maxSubArray(int[] x) {
if(x==null || x.length==0) return 0;
int sum = 0;
int max = x[0];
for (int i = 0; i < x.length; i++) {
if(sum>=0){
sum = sum+x[i];
}else{
sum=x[i];
}
if(sum>max){
max = sum; }
}
// for (int i = 0; i < x.length; i++) {
// for (int j = i; j < x.length; j++) {
// for (int k = i; k <= j; k++) {
// sum = sum + x[k];
// }
// if(MaxSum<sum){
// MaxSum = sum;
// }
// sum=0;
// }
// }
return max;
}
}
Leetcode_53_Maximum Subarray的更多相关文章
- [LeetCode] Maximum Size Subarray Sum Equals k 最大子数组之和为k
Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If t ...
- [LeetCode] Minimum Size Subarray Sum 最短子数组之和
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- [LeetCode] Maximum Product Subarray 求最大子数组乘积
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [LeetCode] Maximum Subarray 最大子数组
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- LeetCode 209 Minimum Size Subarray Sum
Problem: Given an array of n positive integers and a positive integer s, find the minimal length of ...
- Leetcode Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [LintCode] Maximum Subarray 最大子数组
Given an array of integers, find a contiguous subarray which has the largest sum. Notice The subarra ...
- LeetCode-53-Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 【leetcode】Maximum Subarray (53)
1. Maximum Subarray (#53) Find the contiguous subarray within an array (containing at least one nu ...
随机推荐
- linux和android开发链接
1.Tracy Mcgrady的专栏冰山一角:linux和Android底层开发,主要是mtk系列点击打开链接 2.郁闷Wednesday:嵌入式linux 单片机 android,点击打开链接 3. ...
- Android碎裂的粒子效果
最近看到一段时间都没怎么更新文章了,一直在学习iOS相关内容.偶然间看到一个碎裂的粒子效果,觉得很有意思,就查了查,参考下网上的思路自己撸了个轮子. 好了,说了这么多,先看看效果吧~ 依惯例,先说下行 ...
- [extjs5学习笔记]第三十八节 sencha CMD 6.0.0.220版本安装
本文地址:http://blog.csdn.net/sushengmiyan/article/details/46740381 简介 sencha cmd 6安装过程不需要UAC控制了对于widnow ...
- 详解EBS接口开发之应收款处理
参考实例参考:杜春阳 R12应收模块收款API研究 (一)应收款常用标准表简介 1.1 常用标准表 如下表中列出了与应收款处理相关的表和说明: 表名 说明 其他信息 AR_BATCHES_ALL ...
- 仿qq最新侧滑菜单
为了后续对这个项目进行优化,比如透明度动画.背景图的位移动画,以及性能上的优化. 我把这个项目上传到github上面,请大家随时关注. github地址https://github.com/sungu ...
- 财务模块多组织,GL, SLA, SOB, COA, BSV, CCID, LE 概念的简单介绍
GL= General Ledger 指的是Oracle 的总帐系统. application_id = 101. 在uk似乎居然还有不同的解释(In the UK, it was refer ...
- request.setAttribute()怎么用的?
request.setAttribute()怎么用的? JSP1代码 String [] test=new String[2]; test[0]="1"; test[1]=&quo ...
- android仿iphone的地区选择
最近项目要做一个,类似淘宝手机客户端的,选择收货地址的三级联动滚动选择组件,下面是它的大致界面截图: 在IOS中有个叫UIPickerView的选择器,并且在dataSource中定义了UIPicke ...
- Java EE 之 过滤器入门学习与总结(1)
使用Filter技术来配合开发会使得开发变得简单起来.简单的一个例子就表现在"乱码问题"上.不使用Filter的话,我们有可能需要为每一个网页设置字符编码集,如request.se ...
- MyEclipse10+Jdk1.7+OSGI+MySql实现数据库的增删改查
开发环境: Windows2008R2 64位+MyEclipse10+jdk1.7.0_67+MySql5.5 软件安装:myeclipse-10.0-offline-installer-windo ...