Leetcode Hot 100 & 560. Subarray Sum Equals K

  References:

  Test point: substring & [ question stem ]

1 Input: nums = [1,1,1], k = 2
2 Output: 2

  To be honest, I was confused by this question. The first thing I thought of was naturally a $O(n^2)$ solution of double-layer loop traversal, which is the official solution one. But using this solution will time out (Python language is like this, someone mentioned it in the comment area), I know it will be rushed, so I don’t write it directly, and after thinking about it for a long time, I still don’t have a good idea, so I just read the answer directly.

  Answer 2 To be honest, it contains a very powerful idea (exchanging space for time, hash table). I think that if people who are new to this question can think of this solution within a day, I will admire it. So delicate and ingenious! And together with Belden, it is a very good example for the test site of Zichuan.

  1. Understanding of Answer 2

  The second answer contains a basic idea (that makes people pat their thighs and regret why they didn't think of it), that is, the sum of consecutive substrings is actually equal to the difference between the sums of two prefix substrings . Two prefix substrings [1, 2, 3] and [1, 2, 3, 4, 5] can be deduced that the sum of the substrings [4, 5] is 9. At this point we do the opposite, if k = 9, when we traverse to 5, that is, pre_sum[4] = 1 + 2 + ... + 5 = 15, we calculate pre_sum[4] - k = 6 , if there is 6 in the prefix sum of [1], [1, 2], [1, 2, 3], [1, 2, 3, 4], then wouldn’t it be beautiful, it’s just right! The following is my own Python implementation. The official solution is not for the python language. I wrote the code myself but it timed out. . . It can be passed by using other people's code, and it feels like his code is more efficient. . . I don't know why, someone else's:

 1 class Solution(object):
 2     def subarraySum(self, nums, k):
 3         """
 4         :type nums: List[int]
 5         :type k: int
 6         :rtype: int
 7         """
 8         d={0:1} ; 
 9         he = ans = 0
10         for v in nums:
11             he  += v
12             ans += d.get(he-k ,0)
13             d[he] = d.get(he,0) + 1
14         return ans

  I write:

 1 class Solution(object):
 2     def subarraySum(self, nums, k):
 3         """
 4         :type nums: List[int]
 5         :type k: int
 6         :rtype: int
 7         """
 8 
 9         n = len(nums)
10 
11         pre_sum_count = {}
12         pre_sum_count[0] = 1
13 
14         pre_sum = 0
15         count = 0
16         for i in range(n):
17             pre_sum += nums[i]
18             if pre_sum - k in pre_sum_count.keys():
19                 count += pre_sum_count[pre_sum - k]
20             
21             if pre_sum not in pre_sum_count.keys():
22                 pre_sum_count[pre_sum] = 1
23             else:
24                 pre_sum_count[pre_sum] += 1
25 
26         return count

  It may be that there are a lot of in judgments. . . But why use get will not have this problem. . . Anyway, I learned another get(XX, 0), which is to get the value of key=XX, and return 0 if it does not exist. I learned another trick.

  reward:

  1. The idea of ​​exchanging space for time
  2. 使用hash散列表代替数组做存储器的思想。人话:数组的索引是非负整数,很多时候有局限性,此时需要用到hash表,在python中对应的实现就是dict

Guess you like

Origin blog.csdn.net/weixin_43590796/article/details/131256441