• 27.2%

https://leetcode.com/problems/range-sum-query-immutable/

Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.

Note:

1. You may assume that the array does not change.
2. There are many calls to sumRange function.

#### cpp

https://discuss.leetcode.com/topic/29206/5-lines-c-4-lines-python

5-lines C++, 4-lines Python

The idea is fairly straightforward: create an array accu that stores the accumulated sum for nums such that accu[i] = nums[0] + … + nums[i - 1] in the initializer of NumArray. Then just return accu[j + 1] - accu[i] in sumRange. You may try the example in the problem statement to convince yourself of this idea.

The code is as follows.

C++

Python

https://discuss.leetcode.com/topic/30269/c-o-1-queries-just-2-extra-lines-of-code

C++ O(1) queries - just 2 extra lines of code

#### python

https://discuss.leetcode.com/topic/29226/a-very-short-python-solution

A very short Python solution

#### java

https://discuss.leetcode.com/topic/29194/java-simple-o-n-init-and-o-1-query-solution

Java simple O(n) init and O(1) query solution