307. Range Sum Query - Mutable Medium
1/**
2 * [307] Range Sum Query - Mutable
3 *
4 * Given an integer array nums, handle multiple queries of the following types:
5 * <ol>
6 * Update the value of an element in nums.
7 * Calculate the sum of the elements of nums between indices left and right inclusive where left <= right.
8 * </ol>
9 * Implement the NumArray class:
10 *
11 * NumArray(int[] nums) Initializes the object with the integer array nums.
12 * void update(int index, int val) Updates the value of nums[index] to be val.
13 * 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]).
14 *
15 *
16 * Example 1:
17 *
18 * Input
19 * ["NumArray", "sumRange", "update", "sumRange"]
20 * [[[1, 3, 5]], [0, 2], [1, 2], [0, 2]]
21 * Output
22 * [null, 9, null, 8]
23 * Explanation
24 * NumArray numArray = new NumArray([1, 3, 5]);
25 * numArray.sumRange(0, 2); // return 1 + 3 + 5 = 9
26 * numArray.update(1, 2); // nums = [1, 2, 5]
27 * numArray.sumRange(0, 2); // return 1 + 2 + 5 = 8
28 *
29 *
30 * Constraints:
31 *
32 * 1 <= nums.length <= 3 * 10^4
33 * -100 <= nums[i] <= 100
34 * 0 <= index < nums.length
35 * -100 <= val <= 100
36 * 0 <= left <= right < nums.length
37 * At most 3 * 10^4 calls will be made to update and sumRange.
38 *
39 */
40pub struct Solution {}
41
42// problem: https://leetcode.com/problems/range-sum-query-mutable/
43// discuss: https://leetcode.com/problems/range-sum-query-mutable/discuss/?currentPage=1&orderBy=most_votes&query=
44
45// submission codes start here
46
47struct NumArray {
48 vec![]
49 }
50
51
52/**
53 * `&self` means the method takes an immutable reference.
54 * If you need a mutable reference, change it to `&mut self` instead.
55 */
56impl NumArray {
57
58 fn new(nums: Vec<i32>) -> Self {
59
60 }
61
62 fn update(&self, index: i32, val: i32) {
63
64 }
65
66 fn sum_range(&self, left: i32, right: i32) -> i32 {
67
68 }
69}
70
71/**
72 * Your NumArray object will be instantiated and called as such:
73 * let obj = NumArray::new(nums);
74 * obj.update(index, val);
75 * let ret_2: i32 = obj.sum_range(left, right);
76 */
77
78// submission codes end
79
80#[cfg(test)]
81mod tests {
82 use super::*;
83
84 #[test]
85 fn test_307() {
86 }
87}
88
Back
© 2025 bowen.ge All Rights Reserved.