[Leetcode 560] Subarray Sum Equals K
Question 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.
Note: The length of the array is in range [1, 20,000]. The range of numbers in the array is [-1000, 1000] and the range of the integer k is [-1e7, 1e7]. Examples Example 1:
Input:nums = [1,1,1], k = 2 Output: 2 Solution Method 1 (TLE) 我一开始想到的解法是使用一个存了dict的array,记录下每个位置上出现的所有可能的sum的数量。这样进行到每一位的时候只需要看一下他前面那个位置的dict,就能得到包含当前位置数字所有可能出现的sum。