-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathsolution-2.py
More file actions
29 lines (24 loc) · 766 Bytes
/
solution-2.py
File metadata and controls
29 lines (24 loc) · 766 Bytes
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
class Solution(object):
def checkSubarraySum(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: bool
"""
if len(nums)<2:
return False
dp = [0]*len(nums)
dp[0]=nums[0]
for i in range(1,len(nums)):
dp[i]=dp[i-1]+nums[i]
if k==0 and dp[i]==0:
return True
elif k and dp[i]%k==0:
return True
for i in range(0,len(nums)-2):
for j in range(i+2,len(nums)):
if dp[j]-dp[i]==0 and k==0:
return True
elif k and (dp[j]-dp[i])%k==0:
return True
return False