forked from wuduhren/leetcode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontinuous-subarray-sum.py
More file actions
executable file
·35 lines (28 loc) · 1.11 KB
/
continuous-subarray-sum.py
File metadata and controls
executable file
·35 lines (28 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Solution(object):
def checkSubarraySum(self, nums, k):
prefixSumEndings = collections.defaultdict(list)
prefixSumEndings[0].append(-1)
prefixSum = 0
for i, n in enumerate(nums):
prefixSum += n
x = prefixSum/k
while x>=0:
p2 = prefixSum-k*x
if len(prefixSumEndings[p2])>0 and prefixSumEndings[p2][0]<i-1:
return True
x -= 1
prefixSumEndings[prefixSum].append(i)
return False
class Solution(object):
def checkSubarraySum(self, nums, k):
if len(nums)==0 or len(nums)==1: return False
prefixSumKRemain = collections.defaultdict(list)
prefixSum = 0
for i, n in enumerate(nums):
prefixSum += n
remain = prefixSum%k
if prefixSum%k==0 and i>=1: return True
if len(prefixSumKRemain[remain])>0 and prefixSumKRemain[remain][0]<i-1:
return True
prefixSumKRemain[remain].append(i)
return False