Accepted Python solution

  • 2

    This solution is not a O(N) look at the comment "# No O(N) cause", i don't know why, can't use "nums[0]" before loop, i got error out of range, so stranger!

    def __init__(self, nums):
        initialize your data structure here.
        :type nums: List[int]
        self.lists = [0]
        # error out of range when use nums[0] here
        # nums[0]
        for i, n in enumerate(nums):
            # No O(N) cause
            if i == 0:
                self.lists.append(self.lists[i] + n)
    def sumRange(self, i, j):
        sum of elements nums[i..j], inclusive.
        :type i: int
        :type j: int
        :rtype: int
        return self.lists[j+1] - self.lists[i]

  • 0

    You can't use nums[0] there because it is legal that they might pass in an empty array for nums

  • 0

    thank you, this need add a condition

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.