303. Range Sum Query - Immutable Easy

@problem@discussion
#Array#Design#Prefix Sum



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


Back
© 2025 bowen.ge All Rights Reserved.