题目

连续子数组求和

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

样例

给定 [-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. JavaIO和JavaNIO

    BIO和NIO BIO在之前的服务器处理模型中,在调用ServerSocket.accept()方法时,会一直阻塞到有客户端连接才会返回,每个客户端连接过来后,服务端都会accept一个新连接,接着启 ...

  2. javascript Date 函数的坑

    Javascrip中对日期的解析.格式化简直是混乱不堪,各种的坑,攻城狮们多小心.   1. 不要用 Date("yyyy-mm-dd") 构造函数解析字符串. IE.firefo ...

  3. HTML:form表单总结,input,select,option,textarea,label

    <form>标签是块级元素. form标签的标准属性有id,class,style,title,lang,xml:lang. 表单能够包含input元素(包含button,checkbox ...

  4. IIFE-js中(function(){…})()立即执行函数写法理解

    介绍IIFE IIFE的性能 使用IIFE的好处 IIFE最佳实践 jQuery优化 在Bootstrap源码(具体请看<Bootstrap源码解析>)和其他jQuery插件经常看到如下的 ...

  5. 感知器算法PLA

    for batch&supervised binary classfication,g≈f <=> Eout(g)≥0 achieved through Eout(g)≈Ein(g ...

  6. WPF:实现主应用程序单一实例运行方式总结

       本文介绍常见的实现主应用程序单一实例运行的几种方式. 方式一: public partial class App : Application { protected override void ...

  7. 1107. Social Clusters (30)

    When register on a social network, you are always asked to specify your hobbies in order to find som ...

  8. HDU 1405 第六周 J题

    Description Tomorrow is contest day, Are you all ready?  We have been training for 45 days, and all ...

  9. MySQL性能优化的最佳20+套经验

      今天,数据库的操作越来越成为整个应用的性能瓶颈了,这点对于Web应用尤其明显.关于数据库的性能,这并不只是DBA才需要担心的事,而这更是我 们程序员需要去关注的事情.当我们去设计数据库表结构,对操 ...

  10. 帝国cms刷洗内容页提示.phome_ecms_news_data_' doesn't exist

    帝国cms后台刷新提示.phome_ecms_news_data_' doesn't exist解决方法: 刷新所有信息内容页面时提示“Table '*.phome_ecms_article_data ...