You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Algorithms/Continuous Subarray Sum/README.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@ Recently I start to learn Python, it's my first solution with Python.
2
2
3
3
If thinking it over in the normal way, the time complexity will be O(n^2), but we can do it linearly.
4
4
5
-
Define an array `sum`, `sum[n]` means the summary of the first n + 1 items, so the summary from `nums[n]` to `nums[m]` is `sum[m] - sum[n - 1]`, we can judge if the value is the multiple of `k`. But the complexity is still O(n^2).
5
+
Define an array `sum`, `sum[n]` means the sum of the first n + 1 items, so the sum from `nums[n]` to `nums[m]` is `sum[m] - sum[n - 1]`, we can judge if the value is the multiple of `k`. But the complexity is still O(n^2).
6
6
7
7
Think it further, define the remainder of `sum[m]` and `k` as `a`, and the one of `sum[n - 1]` and `k` as b, if `a` is equal to `b`, `sum[m] - sum[n - 1]` will be the multiple of `k`.
8
8
@@ -11,6 +11,6 @@ So it will be easy, just judge `sum[m] % k` is exsited or not, using Set to hash
11
11
Two points to care:
12
12
13
13
- k is 0
14
-
- the continuous subarray of size is at least 2, so we should define a variable `pre` to store the previous summary(maybe summary % k), and add it to the set later on.
14
+
- the continuous subarray of size is at least 2, so we should define a variable `pre` to store the previous sum(maybe sum % k), and add it to the set later on.
0 commit comments