How to solve the problem of immutability of regions and retrieved arrays by leetcode
Editor to share with you how leetcode solves the immutable problem of regions and retrieval arrays. I hope you will get something after reading this article. Let's discuss it together.
I. the content of the topic
Given an integer array nums, find the sum of the elements in the array from index I to j (I ≤ j), including I and j points.
Implement the NumArray class:
NumArray (int [] nums) initializes objects using array nums
Int sumRange (int I, int j) returns the sum of the elements in the array nums from index I to j (I ≤ j), including I and j points (that is, sum (nums [I], nums [I + 1],..., nums [j]))
Example:
Enter:
["NumArray", "sumRange", "sumRange", "sumRange"]
[[- 2, 0, 3,-5, 2,-1]], [0, 2], [2, 5], [0, 5]]
Output:
[null, 1,-1,-3]
Explanation:
NumArray numArray = new NumArray ([- 2,0,3,-5,2,-1])
NumArray.sumRange (0,2); / / return 1 ((- 2) + 0 + 3)
NumArray.sumRange (2,5); / / return-1 (3 + (- 5) + 2 + (- 1))
NumArray.sumRange (0,5); / / return-3 ((- 2) + 0 + 3 + (- 5) + 2 + (- 1))
Tip:
0