Class NumArray

  • All Implemented Interfaces:

    
    public final class NumArray
    
                        

    307 - Range Sum Query - Mutable.

    Medium

    Given an integer array nums, handle multiple queries of the following types:

    • Update the value of an element in nums.

    • Calculate the sum of the elements of nums between indices left and right inclusive where left <= right.

    Implement the NumArray class:

    • NumArray(int[] nums) Initializes the object with the integer array nums.

    • void update(int index, int val) Updates the value of nums[index] to be val.

    • int sumRange(int left, int right) Returns the sum of the elements of nums between indices left and right inclusive (i.e. nums[left] + nums[left + 1] + ... + nums[right]).

    Example 1:

    Input "NumArray", "sumRange", "update", "sumRange" [[1, 3, 5], 0, 2, 1, 2, 0, 2]

    Output: null, 9, null, 8

    Explanation: 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

    Constraints:

    • <code>1 <= nums.length <= 3 * 10<sup>4</sup></code>

    • -100 &lt;= nums[i] &lt;= 100

    • 0 &lt;= index &lt; nums.length

    • -100 &lt;= val &lt;= 100

    • 0 &lt;= left &lt;= right &lt; nums.length

    • At most <code>3 * 10<sup>4</sup></code> calls will be made to update and sumRange.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      final Unit update(Integer index, Integer val)
      final Integer sumRange(Integer left, Integer right)
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait