题目

连续子数组求和

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

样例

给定 [-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. 【转】 (C#)利用Aspose.Cells组件导入导出excel文件

    Aspose.Cells组件可以不依赖excel来导入导出excel文件: 导入: public static System.Data.DataTable ReadExcel(String strFi ...

  2. linux的7种运行级别

    Linux有7个运行级别(runlevel)运行级别0:系统停机状态,系统默认运行级别不能设为0,否则不能正常启动运行级别1:单用户工作状态,root权限,用于系统维护,禁止远程登陆运行级别2:多用户 ...

  3. PHP CodeIgniter(CI)去掉 index.php

    去掉CodeIgniter(CI)默认url中的index.php的步骤: 1.打开apache的配置文件,conf/httpd.conf : LoadModule rewrite_module mo ...

  4. Flash Activex NPAPI PPAPI 各种网页插件完整安装包下载地址

    内容全部是自己手工原创写作的参考内容,完全排除从其他网站COPY的内容信息.如有雷同实属巧合.   奉献给有需求的人士,也给各位解决FLASH安装头疼的问题,正常在线下载安装运气不好的安装半天.运气好 ...

  5. Ubuntu下codeblocks汉化

    code::blocks是一个十分好用编辑环境,一个在手,无所不能,为了更好的支持中文,我列出了汉化的方法: 1下载中文汉化包:http://pan.baidu.com/s/1hqvNZbI 2.解压 ...

  6. android LayoutInflater.inflate()的参数介绍

    LayoutInflater.inflate()的作用就是将一个xml定义的布局文件实例化为view控件对象: 与findViewById区别: LayoutInflater.inflate是加载一个 ...

  7. SqlServer维护计划

    http://blog.csdn.net/yunye114105/article/details/6594826

  8. Linq to XML 之XElement的Descendants方法的新发现

    C#操作XML的方法有很多,但个人认为最方便的莫过于Linq to XML了,特别是XElement的Descendants方法是我最常用的一个方法. 这个方法可以根据节点名(Name)找到当前调用的 ...

  9. C# 清楚Cookies

    //销毁Cookies中的数据 if (Request.Cookies["Ticket"] != null) { HttpCookie mycookie; mycookie = R ...

  10. FindWindow()&&FindWindowEx

    这个函数呢,我一般用来自动刷刷网页啥的比如我最近就在刷52破解的在线时间,好啦怎么用是你自己的事情. FindWindow()主要用来获取目标句柄 或着说窗口的权限 HWND FindWindow( ...