[LeetCode]Continuous Subarray Sum
523. Continuous Subarray Sum
class Solution(object):
def checkSubarraySum(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: bool
"""
if len(nums) < 2:
return False
if k == 0:
return sum(nums) == 0 and len(nums) >= 2
setS = {0:-1}
total = 0
for i, num in enumerate(nums):
total += num
if total%k not in setS:
setS[total%k] = i
elif setS[total%k] != i-1:
return True
return False
关注公众号:数据结构与算法那些事儿,每天一篇数据结构与算法

浙公网安备 33010602011771号