[leetcode523]Continuous Subarray Sum
2017-05-17 17:17
477 查看
https://leetcode.com/problems/continuous-subarray-sum/#/solutions
class Solution(object):
def checkSubarraySum(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: bool
"""
if k == 0:
for i in range(0, len(nums) - 1):
if nums[i] == 0 and nums[i+1] == 0:
return True
return False
k = abs(k)
if len(nums) >= k * 2:
return True
sum = [0]
for x in nums:
sum.append((sum[-1] + x) % k)
Dict = {}
for i in range(0, len(sum)):
if Dict.has_key(sum[i]):
if i - Dict[sum[i]] > 1:
return True
else:
Dict[sum[i]] = i
return False
class Solution(object):
def checkSubarraySum(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: bool
"""
if k == 0:
for i in range(0, len(nums) - 1):
if nums[i] == 0 and nums[i+1] == 0:
return True
return False
k = abs(k)
if len(nums) >= k * 2:
return True
sum = [0]
for x in nums:
sum.append((sum[-1] + x) % k)
Dict = {}
for i in range(0, len(sum)):
if Dict.has_key(sum[i]):
if i - Dict[sum[i]] > 1:
return True
else:
Dict[sum[i]] = i
return False
相关文章推荐
- LeetCode 523 Continuous Subarray Sum (同余)
- LeetCode - 523 - Continuous Subarray Sum
- LeetCode 523: Continuous Subarray Sum
- LeetCode: -Dynamic Programming-Continuous Subarray Sum[523]
- leetcode523 Continuous Subarray Sum
- 【LeetCode523】. Continuous Subarray Sum
- 523. Continuous Subarray Sum
- 523. Continuous Subarray Sum
- 523. Continuous Subarray Sum
- 523. Continuous Subarray Sum
- LeetCode Continuous Subarray Sum
- 523. Continuous Subarray Sum
- 523. Continuous Subarray Sum
- 523. Continuous Subarray Sum
- 523. Continuous Subarray Sum
- 523. Continuous Subarray Sum
- 523. Continuous Subarray Sum
- [LeetCode] Continuous Subarray Sum 连续的子数组之和
- 523. Continuous Subarray Sum
- Math-523-Continuous Subarray Sum