struct NumArray {
sum: 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 sum = vec![0_i32; nums.len() + 1];
sum[0] = 0;
for i in 1..sum.len() {
sum[i] = sum[i - 1] + nums[i - 1];
}
return Self {
sum
};
}
fn sum_range(&self, left: i32, right: i32) -> i32 {
return self.sum[(right+1) as usize] - self.sum[left as usize]
}
}
/**
* Your NumArray object will be instantiated and called as such:
* let obj = NumArray::new(nums);
* let ret_1: i32 = obj.sum_range(left, right);
*/
结果