mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
44 lines
1.9 KiB
HTML
44 lines
1.9 KiB
HTML
<p>Given an integer array <code>nums</code>, handle multiple queries of the following types:</p>
|
|
|
|
<ol>
|
|
<li><strong>Update</strong> the value of an element in <code>nums</code>.</li>
|
|
<li>Calculate the <strong>sum</strong> of the elements of <code>nums</code> between indices <code>left</code> and <code>right</code> <strong>inclusive</strong> where <code>left <= right</code>.</li>
|
|
</ol>
|
|
|
|
<p>Implement the <code>NumArray</code> class:</p>
|
|
|
|
<ul>
|
|
<li><code>NumArray(int[] nums)</code> Initializes the object with the integer array <code>nums</code>.</li>
|
|
<li><code>void update(int index, int val)</code> <strong>Updates</strong> the value of <code>nums[index]</code> to be <code>val</code>.</li>
|
|
<li><code>int sumRange(int left, int right)</code> Returns the <strong>sum</strong> of the elements of <code>nums</code> between indices <code>left</code> and <code>right</code> <strong>inclusive</strong> (i.e. <code>nums[left] + nums[left + 1] + ... + nums[right]</code>).</li>
|
|
</ul>
|
|
|
|
<p> </p>
|
|
<p><strong class="example">Example 1:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input</strong>
|
|
["NumArray", "sumRange", "update", "sumRange"]
|
|
[[[1, 3, 5]], [0, 2], [1, 2], [0, 2]]
|
|
<strong>Output</strong>
|
|
[null, 9, null, 8]
|
|
|
|
<strong>Explanation</strong>
|
|
NumArray numArray = new NumArray([1, 3, 5]);
|
|
numArray.sumRange(0, 2); // return 1 + 3 + 5 = 9
|
|
numArray.update(1, 2); // nums = [1, 2, 5]
|
|
numArray.sumRange(0, 2); // return 1 + 2 + 5 = 8
|
|
</pre>
|
|
|
|
<p> </p>
|
|
<p><strong>Constraints:</strong></p>
|
|
|
|
<ul>
|
|
<li><code>1 <= nums.length <= 3 * 10<sup>4</sup></code></li>
|
|
<li><code>-100 <= nums[i] <= 100</code></li>
|
|
<li><code>0 <= index < nums.length</code></li>
|
|
<li><code>-100 <= val <= 100</code></li>
|
|
<li><code>0 <= left <= right < nums.length</code></li>
|
|
<li>At most <code>3 * 10<sup>4</sup></code> calls will be made to <code>update</code> and <code>sumRange</code>.</li>
|
|
</ul>
|