본문 바로가기
Leetcode

303. Range Sum Query - Immutable

by YGSEO 2020. 8. 16.
728x90
class NumArray(object):
    def __init__(self, nums):
        """
        initialize your data structure here.
        :type nums: List[int]
        """
        self.sums = [0] * (len(nums) + 1)
        for i in xrange(len(nums)):
            self.sums[i+1] = self.sums[i] + nums[i]

    def sumRange(self, i, j):
        """
        sum of elements nums[i..j], inclusive.
        :type i: int
        :type j: int
        :rtype: int
        """
        return self.sums[j+1] - self.sums[i]

출처:

leetcode.com/problems/range-sum-query-immutable/discuss/75319/Share-my-Python-solution-O(n)-for-init-and-O(1)-for-query

728x90

'Leetcode' 카테고리의 다른 글

[LeetCode] Two Sum 파이썬 (dict, if ~ in)  (0) 2021.03.25
198. House Robber  (0) 2020.08.16
53. Maximum Subarray  (0) 2020.08.16
70. Climbing Stairs  (0) 2020.08.15
392. Is Subsequence  (0) 2020.08.15

댓글