Subarray Sum

给定一个整数数组,找到和为零的子数组。你的代码应该返回满足要求的子数组的起始位置和结束位置。比如给出 [-3, 1, 2, -3, 4],返回[0, 2] 或者 [1, 3].

Lintcode上的一道题目,这一题我开始想到的是brute force的方法,求出所有子数组,并判断和是否为0。子数组一共有n*(n-1)/2个,如果每次再求和,则复杂度为O(n^3),于是想到使用DP缓存中间结果,不过无论用一维数组还是二维数组,最后都是超时的结果。所以说如果暴力解法都是多项式时间的话,则使用DP的可能性很低。所以这题得另想思路。

Hashmap感觉是解这种问题的通用套路,这题看似不可以,其实用很巧的思路转换一下就可以。如果有一段子数组和为0,比如从i到j,则第一个数到第i个数的和等于第一个数到第j个数的和的值。因为中间这段为0。所以从可以依次向后求解,并将和作为键保存到词典中,键对应的值为index i。一旦后面有求和值为字典中的键值,则前一个index+1至现在的index为该和为0的数组。但是有一种情况如果从第一个数组开始的子序列和为0,则需要如何处理?所以一个好的办法是先在字典中存入{0:-1}这一对,方便后序求解。代码如下:

class Solution:
    """
    @param nums: A list of integers
    @return: A list of integers includes the index of the first number
             and the index of the last number
    """
    def subarraySum(self, nums):
        map = {0:-1}
        sum = 0
        for i in xrange(len(nums)):
            sum += nums[i]
            if map.has_key(sum):
                return [map[sum]+1,i]
            map[sum] = i
        return 
时间: 2024-10-10 07:12:18

Subarray Sum的相关文章

560. Subarray Sum Equals K

Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k. Example 1: Input:nums = [1,1,1], k = 2 Output: 2 Note: The length of the array is in range [1, 20,000]. The range of numbers

Subarray Sum Closet

Given an integer array, find a subarray with sum closest to zero. Return the indexes of the first number and last number. Given [-3, 1, 1, -3, 5], return [0, 2], [1, 3], [1, 1], [2, 2] or [0, 4]. 这题求和最接近0的子数组,属于Subarray Sum的follow up.思路也很近似,每次求的是当前位置

Leetcode: Maximum Size Subarray Sum Equals k

Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If there isn't one, return 0 instead. Example 1: Given nums = [1, -1, 5, -2, 3], k = 3, return 4. (because the subarray [1, -1, 5, -2] sums to 3 and is th

Subarray Sum Closest

Question Given an integer array, find a subarray with sum closest to zero. Return the indexes of the first number and last number. Given [-3, 1, 1, -3, 5], return [0, 2], [1, 3],[1, 1], [2, 2] or [0, 4]. Answer 这道题延续Subarray Sum的思路,即将[0, i]的sum存起来.这里

LeetCode OJ 209. Minimum Size Subarray Sum

Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead. For example, given the array [2,3,1,2,4,3] and s = 7,the subarray [4,3] has the minimal

[LeetCode] Minimum Size Subarray Sum 解题思路

Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead. For example, given the array [2,3,1,2,4,3] and s = 7,the subarray [4,3] has the minimal

leetcode_209题——Minimum Size Subarray Sum(两个指针)

Minimum Size Subarray Sum Total Accepted: 10318 Total Submissions: 44504My Submissions Question Solution Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, r

Continuous Subarray Sum II(LintCode)

Continuous Subarray Sum II Given an circular integer array (the next element of the last element is the first element), find a continuous subarray in it, where the sum of numbers is the biggest. Your code should return the index of the first number a

LeetCode:Continuous Subarray Sum

523. Continuous Subarray Sum Add to List Given a list of non-negative numbers and a target integer k, write a function to check if the array has a continuous subarray of size at least 2 that sums up to the multiple of k, that is, sums up to n*k where

Continuous Subarray Sum II

Given an circular integer array (the next element of the last element is the first element), find a continuous subarray in it, where the sum of numbers is the biggest. Your code should return the index of the first number and the index of the last nu