本文是在学习中的总结,欢迎转载但请注明出处: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. Android文件大头10G

    这个玩意直接10G....记录下. C:\Users\xn\AppData\Local\Android\sdk\system-images\android-23

  2. Swift中的可选协议和方法的历史渊源

    @objc protocol Transaction { func commit() -> Bool optional func isComplete() -> Bool } 以上协议被标 ...

  3. 2016年年终CSDN博客总结

    2015年12月1日,结束了4个月的尚观嵌入式培训生涯,经过了几轮重重面试,最终来到了伟易达集团.经过了长达3个月的试用期,正式成为了伟易达集团的助理工程师. 回顾一年来的学习,工作,生活.各种酸甜苦 ...

  4. 仿爱奇艺视频,腾讯视频,搜狐视频首页推荐位轮播图(二)之SuperIndicator源码分析

    转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼:http://blog.csdn.net/hejjunlin/article/details/52510431 背景:仿爱奇艺视频,腾讯视频 ...

  5. React Native实现一个自定义模块

    概述 在 前期介绍React Native 项目结构的时候,我们讲解过React的项目组成,其中说过 node_modules 文件夹,这是一个存放 node 模块的地方.我们知道React是用npm ...

  6. qq安全原理

    故事总要有缘由,那么这个故事的缘由就是,当我以前写了一个获取其它进程密码框密码的时候(前几篇博客中有描述),我抱着试一试的心情去试探了一下能不能得到 QQ 的密码,当我抓到密码框的句柄,然后输入给程序 ...

  7. Eclipse中设置VM参数

    eclipse.ini -Xms256m //设置堆最小值 -Xmx1024m //设置堆最大值 Eclipse 做JVM 的分析时,需要动态设置JVM的参数来进行各种测试, 可以在下图地方进行设置 ...

  8. SYBASE的select into与insert into使用和区别

    对于表的部分或全部字段的复制,Sybase数据库提供了两种方式:select into和insert into. select into: 语法:select  value1, value2, val ...

  9. java多线程的编程实例

    java中可有两种方式实现多线程: 一种是继承Thread类: 一种是实现Runnable接口: Thread类 是在java.lang包中定义的.一个类只要继承了Thread类同时覆写了本类中的ru ...

  10. Android初级教程反射+AIDL+内容观察者监控黑名单号码代码模板

    对于想要拦截一些莫名的陌生号码,就需要电话拦截功能与删除其电话记录功能.拦截的主要业务逻辑,分别是在一个服务里面进行:1.注册电话监听:2.取消注册电话监听(当然注册于取消是在服务里面建立一个广播接收 ...