本文是在学习中的总结,欢迎转载但请注明出处: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的更多相关文章

  1. [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 ...

  2. [LeetCode] Minimum Size Subarray Sum 最短子数组之和

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  3. [LeetCode] Maximum Product Subarray 求最大子数组乘积

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

  4. [LeetCode] Maximum Subarray 最大子数组

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

  5. LeetCode 209 Minimum Size Subarray Sum

    Problem: Given an array of n positive integers and a positive integer s, find the minimal length of ...

  6. Leetcode Maximum Product Subarray

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

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

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

  8. LeetCode-53-Maximum Subarray

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

  9. 【leetcode】Maximum Subarray (53)

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

随机推荐

  1. 在linux系统中I/O 调度的选择

        I/O 调度算法再各个进程竞争磁盘I/O的时候担当了裁判的角色.他要求请求的次序和时机做最优化的处理,以求得尽可能最好的整体I/O性能. 在linux下面列出4种调度算法 CFQ (Compl ...

  2. java http post tomcat解除 长度限制

    1.    Get方法长度限制 Http Get方法提交的数据大小长度并没有限制,HTTP协议规范没有对URL长度进行限制.这个限制是特定的浏览器及服务器对它的限制. 如:IE对URL长度的限制是20 ...

  3. linux:如何指定进程运行的CPU

    coolshell最新的文章<性能调优攻略>在"多核CPU调优"章节,提到"我们不能任由操作系统负载均衡,因为我们自己更了解自己的程序,所以,我们可以手动地为 ...

  4. 2014 BDTC 参会有感

    中国大数据技术大会(Big Data Technology Conference,BDTC)是目前国内最具影响.规模最大的大数据领域的技术盛会.大会的前身是Hadoop中国云计算大会(Hadoop i ...

  5. 第三方开源动画库EasyAnimation中一个小bug的修复

    看过iOS动画之旅的都知道,其中在最后提到一个作者写的开源动画库EasyAnimation(以下简称EA). EA对CoreAnimation中的view和layer动画做了更高层次的包装和抽象,使得 ...

  6. PGM:概率论基础知识

    http://blog.csdn.net/pipisorry/article/details/52459847 概率图模型PGM:概率论基础知识 独立性与条件独立性 独立性 条件独立性 也就是表示给定 ...

  7. SQLite Where 子句(http://www.w3cschool.cc/sqlite/sqlite-where-clause.html)

    SQLite Where 子句 SQLite的 WHERE 子句用于指定从一个表或多个表中获取数据的条件. 如果满足给定的条件,即为真(true)时,则从表中返回特定的值.您可以使用 WHERE 子句 ...

  8. Java继承时的初始化顺序

    Java程序在启动和运行时,需要首先完成初始化的工作.在涉及到继承.static成员变量等因素时,初始化的顺序就复杂起来.下面以一个例子说明继承时的Java初始化顺序. 例子: class Insec ...

  9. [ExtJS5学习笔记]第二十节 Extjs5配合数组的push方法,动态创建并加载组件

    本文地址:http://blog.csdn.net/sushengmiyan/article/details/39226773 官方例子:http://docs.sencha.com/extjs/5. ...

  10. React Native网络请求

    很多移动应用都需要从远程地址中获取数据或资源.你可能需要给某个REST API发起POST请求以提交用户数据,又或者可能仅仅需要从某个服务器上获取一些静态内容--以下就是你会用到的东西.新手可以对照这 ...