leetcode——327. 区间和的个数

超出时间了。。。

class Solution(object):
    def countRangeSum(self, nums, lower, upper):
        """
        :type nums: List[int]
        :type lower: int
        :type upper: int
        :rtype: int
        """
        i=0
        n=0
        while i<len(nums):
            j=i
            while j<len(nums):
                s=sum(nums[i:j+1])
                if s>=lower and s<=upper:
                    n+=1
                    j+=1
                else:
                    if s>upper:
                        j+=1
                        while j<len(nums) and nums[j]>=0:
                            j+=1
                    elif s<lower:
                        j+=1
                        while j<len(nums) and nums[j]<=0:
                            j+=1
            i+=1
        return n

猜你喜欢

转载自www.cnblogs.com/taoyuxin/p/11831085.html