-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrange-sum-query-immutable.rs
More file actions
42 lines (35 loc) · 960 Bytes
/
Copy pathrange-sum-query-immutable.rs
File metadata and controls
42 lines (35 loc) · 960 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#![allow(dead_code, unused, unused_variables)]
fn main() {}
struct Solution;
/**
* Your NumArray object will be instantiated and called as such:
* let obj = NumArray::new(nums);
* let ret_1: i32 = obj.sum_range(left, right);
*/
struct NumArray {
sums: Vec<i32>,
}
/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl NumArray {
fn new(nums: Vec<i32>) -> Self {
let mut sums = Vec::with_capacity(nums.len());
for i in 0..=nums.len() {
if i == 0 {
sums.push(0);
} else {
sums.push(nums[i] + sums[i - 1]);
}
}
Self { sums }
}
fn sum_range(&self, left: i32, right: i32) -> i32 {
if left == 0 {
self.sums[right as usize]
} else {
self.sums[right as usize] - self.sums[left as usize - 1usize]
}
}
}