Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to solve the problem of immutability of regions and retrieved arrays by leetcode

Shulou Source: shulou.com Published: 2022-06-01 13:20:48 10月04日 Update

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

Tags: Array element region problem variable search sum number article index scope that is code content dynamic done object idea integer method Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Shulou Technology macOS vpn Shulou Tech Info