题目

连续子数组求和

给定一个整数数组,请找出一个连续子数组,使得该子数组的和最大。输出答案时,请分别返回第一个数字和最后一个数字的值。(如果两个相同的答案,请返回其中任意一个)

样例

给定 [-3, 1, 3, -3, 4], 返回[1,4].

解题

法一:直接暴力,时间复杂度O(N2),时间超时

public class Solution {
/**
* @param A an integer array
* @return A list of integers includes the index of the first number and the index of the last number
*/
public ArrayList<Integer> continuousSubarraySum(int[] A) {
// Write your code here
ArrayList<Integer> result = new ArrayList<Integer>();
int max = Integer.MIN_VALUE;
for(int i = 0;i<A.length;i++){
int sum = 0;
for(int j = i;j<A.length; j++){
sum += A[j];
if(sum>max){
max = sum;
result.clear();
result.add(i);
result.add(j);
}
}
}
return result;
}
}

Java Code

然后想到有过求最大子数组的和,只是返回最大子数组对于的和,本题是返回子数组的开始和结束的下标.

根据之前做题,突然想到,通过定义一个数组,来保存子数组的右边界是该元素时候的最大和,求出所有和的最大值就是最大子数组和的最大值.

public class Solution {
/**
* @param nums: A list of integers
* @return: A integer indicate the sum of max subarray
*/
public int maxSubArray(int[] nums) {
// write your code
int max = Integer.MIN_VALUE;
int d[] = new int[nums.length];// 以i结束的元素的最大子数组之和
d[0] = nums[0];
max = d[0];
for(int i=1 ;i<nums.length ; i++){
d[i] = Math.max(d[i-1]+nums[i],nums[i]);
max = Math.max(d[i],max);
// System.out.println(d[i]);
}
return max;
}
}

Java Code

可以看出,上面的数组其实可以换成两个变量的.之前看的网上的程序就是定义两个变量的.

本题是求的最大子数组的两个边界下标.

根据上面的思想,这个数组的下标是该子数组的右下标,而数组的值是子数组的左下标.。。。。然而发现实现不了。。。

网上就找到下面的程序,很好理解,但是自己写就会写乱。。。

public class Solution {
/**
* @param A an integer array
* @return A list of integers includes the index of the first number and the index of the last number
*/
public ArrayList<Integer> continuousSubarraySum(int[] A) {
// Write your code here
ArrayList<Integer> result = new ArrayList<Integer>();
int begin = 0;
int end = 0;
int sum = A[0];
int maxsum = A[0];
int maxbegin = 0;
int maxend = 0;
for(int i = 1;i<A.length;i++){
if(sum <= 0){
if(A[i] > sum){
begin = i;
end = i;
sum = A[i];
}
if(A[i] >= maxsum){
maxbegin = i;
maxend = i;
maxsum = A[i];
}
}else{
sum +=A[i];
if(sum> 0){
end = i;
}
if(sum > maxsum){
maxbegin = begin;
maxend = i;
maxsum = sum;
}
}
}
result.add(maxbegin);
result.add(maxend);
return result;
}
}

Java Code

总耗时: 11178 ms

定义两对开始和结束的最大路径,一个用来保存当前路径的最大值得开始结束位置,一个用来保存所有路径的中最大值得开始结束位置。

当sum<0的时候 并且A[i] > sum 更新begin End sum

  当A[i] >=maxsum时候更新maxbegin maxend maxsum

当sum>0  sum+=A[i]

  此时若sum > 0 end 更新为当前的 i

  若 sum>maxsum 时候更新maxbegin maxend maxsum

最后结束的就是答案了

class Solution:
# @param {int[]} A an integer array
# @return {int[]} A list of integers includes the index of the
# first number and the index of the last number
def continuousSubarraySum(self, A):
# Write your code here begin,end,suma = 0,0,A[0]
maxbegin,maxend,maxsum = 0,0,A[0]
for i in range(1,len(A)):
if suma < 0:
if A[i] > suma :
begin = i
end = i;
suma = A[i]
if A[i]>= maxsum:
maxbegin = i
maxend = i;
maxsum = A[i]
else:
suma +=A[i]
if suma>0:
end = i
if suma > maxsum:
maxbegin = begin
maxend = i
maxsum = suma
return [maxbegin,maxend]

Python Code

总耗时: 673 ms

lintcode :continuous subarray sum 连续子数组之和的更多相关文章

  1. [LintCode] Continuous Subarray Sum 连续子数组之和

    Given an integer array, find a continuous subarray where the sum of numbers is the biggest. Your cod ...

  2. [leetcode]523. Continuous Subarray Sum连续子数组和(为K的倍数)

    Given a list of non-negative numbers and a target integer k, write a function to check if the array ...

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

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

  4. Minimum Size Subarray Sum 最短子数组之和

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

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

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

  6. [LeetCode] 930. Binary Subarrays With Sum 二元子数组之和

    In an array A of 0s and 1s, how many non-empty subarrays have sum S? Example 1: Input: A = [1,0,1,0, ...

  7. lintcode循环数组之连续子数组求和

    v 题目:连续子数组求和 II 给定一个整数循环数组(头尾相接),请找出一个连续的子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的值.如果多个答案,请返回其中任意一个. ...

  8. [LintCode] Continuous Subarray Sum II

    Given an integer array, find a continuous rotate subarray where the sum of numbers is the biggest. Y ...

  9. 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大

    Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...

随机推荐

  1. Linux ThunderBird Exchange 过期

    在Linux上只用Web版处理邮件,就是因为找不到太好的能支持Exchange的邮件客户端.在网上无意中发现了ExQuilla这个Thunderbird的插件,试用了一下还是不错的,很方便,不过只能免 ...

  2. Entity Framework 并发处理借鉴

    如下博客 http://www.cnblogs.com/Bce-/p/3725868.html

  3. mysql手工注入

    以下是mynona本人原创的,奉献给大家,不要小看数据库注入 参考: http://www.daydaydata.com/help/sql/advance/limit.html http://www. ...

  4. Eclipse 代码提示功能设置。

    1.        解决实例化时自动补全不必要的单词问题 2.        以MyEclipse 6.5重新配图 鉴 于网上的批评之声甚大,我只想说明我的想法:这样的增强代码提示,最终是用来辅助我们 ...

  5. cordova ios

    使用Cordova进行iOS开发 (环境配置及基本用法) 字数1426 阅读3044 评论0 喜欢5 安装Cordova CLI 1. cordova的安装: 1.1 安装cordova需要先安装no ...

  6. 用户登录密码RSA加密后传输的实现,非明文密码传输

    在用户登录页面,用户输入密码后,在传送到服务器端时,为防止在密码传送过程中,被如360这种东东给拦截到, 需要在传送前对密码进行加密,然后再传送! 利用RSA加密,在客户端使用公钥对密码进行加密,在服 ...

  7. JDBC 连接数据库

          JAVA使用JDBC访问数据库的步骤: 1.     得到数据库驱动程序   (导包) 2.     创建数据库连接  3.     执行SQL语句 4.     得到结果集 5.     ...

  8. hadoop集群默认配置和常用配置【转】

    转自http://www.cnblogs.com/ggjucheng/archive/2012/04/17/2454590.html 获取默认配置 配置hadoop,主要是配置core-site.xm ...

  9. C#实现发送邮件——核心部分代码

    在KS系统中有个发送邮件的功能需要做上网查阅资料以后,通过自己的部分修改实现了发送邮件的功能话不多说先来个界面: 邮件发送分一下步骤: 1.smtp服务信息设置 2.验证发件人信息 3.添加附件 4. ...

  10. SQL统计——按照各种维度

    在SQLserver中可以按照各种维度进行统计,实现与EXCLE一样强大的功能. --========================== --Blog:<奔跑的金鱼> --Desc:&l ...