您的位置:首页 > 其它

[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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: