-
Notifications
You must be signed in to change notification settings - Fork 0
Leetcode subarray sum equals k #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
20e3c76
a543d98
d55c803
10b1363
1d9e85d
ec1091c
5eb31e4
1c3e8fd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # Arrays | ||
|
|
||
| + [Subarray Sum Equals K](#subarray-sum-equals-k) | ||
|
|
||
| ## Subarray sum equals k | ||
|
|
||
| https://leetcode.com/problems/subarray-sum-equals-k/ | ||
|
|
||
| ```python | ||
| from collections import defaultdict | ||
|
|
||
|
|
||
| class Solution(object): | ||
| def subarraySum(self, nums, k): | ||
| """ | ||
| :type nums: List[int] | ||
| :type k: int | ||
| :rtype: int | ||
| """ | ||
|
|
||
| _list, _dict, sums_count = [0] + [sum(nums[:index + 1]) for index in range(len(nums))], defaultdict(int), 0 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Придется упростить через кумулятивную сумму. Слишком сложно да и еще с подсчетом сумм
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Я хотел добавить импорт, но leetcode как-то не может это сделать, поэтому переписал в виде генератора списка. Можно сделать и через reduce, наверное. С reduce, может, попроще станет, стоит ли? |
||
| for i in range(len(nums)): | ||
| _dict[_list[i]] += 1 | ||
| sums_count += _dict[_list[i + 1] - k] | ||
| return sums_count | ||
|
|
||
| ``` | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
На каждую задачу отдельный Pull Request
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Vivelapaix разделил на три ветки.