523. Continuous Subarray Sum
Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 31% Topics: Array, Hash Table, Math, Prefix Sum
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(n)
# Space: O(k)
class Solution(object):
def checkSubarraySum(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: bool
"""
count = 0
lookup = {0: -1}
for i, num in enumerate(nums):
count += num
if k:
count %= k
if count in lookup:
if i - lookup[count] > 1:
return True
else:
lookup[count] = i
return False
Solution from kamyu104/LeetCode-Solutions · MIT