[LeetCode] 560. Subarray Sum Equals K_Medium

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:

  1. The length of the array is in range [1, 20,000].
  2. The range of numbers in the array is [-1000, 1000] and the range of the integer k is [-1e7, 1e7].

这个题目思路是利用cumulative sum, 前面数字之和去计算. 参考Leetcode 原题Solution.

建一个diction, 记录 sum 出现过的次数, 然后每次用sum - target, 如果存在在dction里面, count += d[sum-target], 最后返回count. 如果直到这个思路之后其实不难, 否则还蛮难想的.

class Solution:
    def subarraySum(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: int
        """

    d, count,s = {0:1}, 0,0  # 最开始有0:1 是因为"" 的sum是0, 所以初始为1
    for num in nums:
        s += num
        if s - k in d:
            count += d[s-k]
        if s not in d:
            d[s] = 1
        else:
            d[s] += 1

    return count    

原文地址:https://www.cnblogs.com/Johnsonxiong/p/9206892.html

时间: 2024-08-27 01:52:02

[LeetCode] 560. Subarray Sum Equals K_Medium的相关文章

[leetcode 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

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

[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 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

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

[LintCode] 1844. subarray sum equals k II

Given an array of integers and an integer k, you need to find the minimum size of continuous subarrays whose sum equals to k, and return its length. if there are no such subarray, return -1. Example Example1 Input: nums = [1,1,1,2] and k = 3 Output:

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

[LeetCode] Maximum Subarray Sum

Dynamic Programming There is a nice introduction to the DP algorithm in this Wikipedia article. The idea is to maintain a running maximum smax and a current summation sum. When we visit each num in nums, addnum to sum, then update smax if necessary o

Map-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

Maximum Size Subarray Sum Equals k LT325

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. Note:The sum of the entire nums array is guaranteed to fit within the 32-bit signed integer range. Example 1: Given