5 2 votes
Article Rating
Subscribe
提醒
guest
4 评论
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
sunznx
// CreateTime: 2021-01-19 00:19:27
class NumArray {
public:
    vector<int> p;


    NumArray(vector<int> nums) {
        p.resize(nums.size()+1);


        for (int i = 0; i < nums.size(); i++) {
            p[i+1] = p[i] + nums[i];
        }
    }


    int sumRange(int i, int j) {
        return p[j+1] - p[i];
    }
};


kitsune

针不辍。。小白表示很友好,前缀和搞定

暴走的楚狂

# python3
# 主要知识点就是prefix/pre-calculated sum.

from itertools import accumulate

class NumArray:

  def __init__(self, nums: List[int]):
    self.nums = [0] + list(accumulate(nums))
     
  def sumRange(self, i: int, j: int) -> int:
    return self.nums[j+1] – self.nums[i]