Skip to content

Commit f65829d

Browse files
authored
feat: add solutions to lc problems: No.303~305 (doocs#2142)
1 parent 9850c03 commit f65829d

File tree

17 files changed

+875
-496
lines changed

17 files changed

+875
-496
lines changed

solution/0300-0399/0303.Range Sum Query - Immutable/README.md

+37-53
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ numArray.sumRange(0, 5); // return -3 ((-2) + 0 + 3 + (-5) + 2 + (-1))
5454

5555
**方法一:前缀和**
5656

57-
前缀和计算公式:`s[i + 1] = s[i] + nums[i]`
57+
我们创建一个长度为 $n + 1$ 的前缀和数组 $s$,其中 $s[i]$ 表示前 $i$ 个元素的前缀和,即 $s[i] = \sum_{j=0}^{i-1} nums[j]$,那么索引 $[left, right]$ 之间的元素的和就可以表示为 $s[right + 1] - s[left]$
5858

59-
初始化的时间复杂度是 $O(n)$,每次查询的时间复杂度是 $O(1)$。其中 $n$ 是数组的长度
59+
初始化前缀和数组 $s$ 的时间复杂度为 $O(n)$,查询的时间复杂度为 $O(1)$。空间复杂度 $O(n)$
6060

6161
<!-- tabs:start -->
6262

@@ -111,17 +111,20 @@ class NumArray {
111111
```cpp
112112
class NumArray {
113113
public:
114-
vector<int> s;
115-
116114
NumArray(vector<int>& nums) {
117115
int n = nums.size();
118116
s.resize(n + 1);
119-
for (int i = 0; i < n; ++i) s[i + 1] = s[i] + nums[i];
117+
for (int i = 0; i < n; ++i) {
118+
s[i + 1] = s[i] + nums[i];
119+
}
120120
}
121121

122122
int sumRange(int left, int right) {
123123
return s[right + 1] - s[left];
124124
}
125+
126+
private:
127+
vector<int> s;
125128
};
126129

127130
/**
@@ -166,7 +169,7 @@ func (this *NumArray) SumRange(left int, right int) int {
166169
*/
167170
var NumArray = function (nums) {
168171
const n = nums.length;
169-
this.s = new Array(n + 1).fill(0);
172+
this.s = Array(n + 1).fill(0);
170173
for (let i = 0; i < n; ++i) {
171174
this.s[i + 1] = this.s[i] + nums[i];
172175
}
@@ -196,7 +199,7 @@ class NumArray {
196199

197200
constructor(nums: number[]) {
198201
const n = nums.length;
199-
this.s = new Array(n + 1).fill(0);
202+
this.s = Array(n + 1).fill(0);
200203
for (let i = 0; i < n; ++i) {
201204
this.s[i + 1] = this.s[i] + nums[i];
202205
}
@@ -218,34 +221,7 @@ class NumArray {
218221

219222
```rust
220223
struct NumArray {
221-
nums: Vec<i32>,
222-
}
223-
224-
/**
225-
* `&self` means the method takes an immutable reference.
226-
* If you need a mutable reference, change it to `&mut self` instead.
227-
*/
228-
impl NumArray {
229-
fn new(nums: Vec<i32>) -> Self {
230-
Self {
231-
nums,
232-
}
233-
}
234-
235-
fn sum_range(&self, left: i32, right: i32) -> i32 {
236-
let (left, right) = (left as usize, right as usize);
237-
self.nums[left..=right].iter().sum::<i32>()
238-
}
239-
}/**
240-
* Your NumArray object will be instantiated and called as such:
241-
* let obj = NumArray::new(nums);
242-
* let ret_1: i32 = obj.sum_range(left, right);
243-
*/
244-
```
245-
246-
```rust
247-
struct NumArray {
248-
sums: Vec<i32>,
224+
s: Vec<i32>,
249225
}
250226

251227
/**
@@ -255,15 +231,15 @@ struct NumArray {
255231
impl NumArray {
256232
fn new(mut nums: Vec<i32>) -> Self {
257233
let n = nums.len();
258-
let mut sums = vec![0; n + 1];
234+
let mut s = vec![0; n + 1];
259235
for i in 0..n {
260-
sums[i + 1] = sums[i] + nums[i];
236+
s[i + 1] = s[i] + nums[i];
261237
}
262-
Self { sums }
238+
Self { s }
263239
}
264240

265241
fn sum_range(&self, left: i32, right: i32) -> i32 {
266-
self.sums[(right + 1) as usize] - self.sums[left as usize]
242+
self.s[(right + 1) as usize] - self.s[left as usize]
267243
}
268244
}/**
269245
* Your NumArray object will be instantiated and called as such:
@@ -276,25 +252,26 @@ impl NumArray {
276252

277253
```c
278254
typedef struct {
279-
int* sums;
255+
int* s;
280256
} NumArray;
281257

282-
NumArray* numArrayCreate(int* nums, int numsSize) {
283-
int* sums = malloc(sizeof(int) * (numsSize + 1));
284-
memset(sums, 0, numsSize + 1);
285-
for (int i = 0; i < numsSize; i++) {
286-
sums[i + 1] = sums[i] + nums[i];
258+
NumArray* numArrayCreate(int* nums, int n) {
259+
int* s = malloc(sizeof(int) * (n + 1));
260+
s[0] = 0;
261+
for (int i = 0; i < n; i++) {
262+
s[i + 1] = s[i] + nums[i];
287263
}
288-
NumArray* res = malloc(sizeof(NumArray));
289-
res->sums = sums;
290-
return res;
264+
NumArray* obj = malloc(sizeof(NumArray));
265+
obj->s = s;
266+
return obj;
291267
}
292268

293269
int numArraySumRange(NumArray* obj, int left, int right) {
294-
return obj->sums[right + 1] - obj->sums[left];
270+
return obj->s[right + 1] - obj->s[left];
295271
}
296272

297273
void numArrayFree(NumArray* obj) {
274+
free(obj->s);
298275
free(obj);
299276
}
300277

@@ -315,20 +292,27 @@ class NumArray {
315292
* @param Integer[] $nums
316293
*/
317294
function __construct($nums) {
318-
$this->sum = [0];
319-
for ($i = 0; $i < count($nums); $i++) {
320-
array_push($this->sum, $this->sum[$i] + $nums[$i]);
295+
$this->s = [0];
296+
foreach ($nums as $x) {
297+
$this->s[] = $this->s[count($this->s) - 1] + $x;
321298
}
322299
}
300+
323301
/**
324302
* @param Integer $left
325303
* @param Integer $right
326304
* @return Integer
327305
*/
328306
function sumRange($left, $right) {
329-
return $this->sum[$right + 1] - $this->sum[$left];
307+
return $this->s[$right + 1] - $this->s[$left];
330308
}
331309
}
310+
311+
/**
312+
* Your NumArray object will be instantiated and called as such:
313+
* $obj = NumArray($nums);
314+
* $ret_1 = $obj->sumRange($left, $right);
315+
*/
332316
```
333317

334318
### **...**

solution/0300-0399/0303.Range Sum Query - Immutable/README_EN.md

+41-51
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ numArray.sumRange(0, 5); // return (-2) + 0 + 3 + (-5) + 2 + (-1) = -3
4646

4747
## Solutions
4848

49+
**Solution 1: Prefix Sum**
50+
51+
We create a prefix sum array $s$ of length $n + 1$, where $s[i]$ represents the prefix sum of the first $i$ elements, that is, $s[i] = \sum_{j=0}^{i-1} nums[j]$. Therefore, the sum of the elements between the indices $[left, right]$ can be expressed as $s[right + 1] - s[left]$.
52+
53+
The time complexity for initializing the prefix sum array $s$ is $O(n)$, and the time complexity for querying is $O(1)$. The space complexity is $O(n)$.
54+
4955
<!-- tabs:start -->
5056

5157
### **Python3**
@@ -95,17 +101,20 @@ class NumArray {
95101
```cpp
96102
class NumArray {
97103
public:
98-
vector<int> s;
99-
100104
NumArray(vector<int>& nums) {
101105
int n = nums.size();
102106
s.resize(n + 1);
103-
for (int i = 0; i < n; ++i) s[i + 1] = s[i] + nums[i];
107+
for (int i = 0; i < n; ++i) {
108+
s[i + 1] = s[i] + nums[i];
109+
}
104110
}
105111

106112
int sumRange(int left, int right) {
107113
return s[right + 1] - s[left];
108114
}
115+
116+
private:
117+
vector<int> s;
109118
};
110119

111120
/**
@@ -150,7 +159,7 @@ func (this *NumArray) SumRange(left int, right int) int {
150159
*/
151160
var NumArray = function (nums) {
152161
const n = nums.length;
153-
this.s = new Array(n + 1).fill(0);
162+
this.s = Array(n + 1).fill(0);
154163
for (let i = 0; i < n; ++i) {
155164
this.s[i + 1] = this.s[i] + nums[i];
156165
}
@@ -180,7 +189,7 @@ class NumArray {
180189

181190
constructor(nums: number[]) {
182191
const n = nums.length;
183-
this.s = new Array(n + 1).fill(0);
192+
this.s = Array(n + 1).fill(0);
184193
for (let i = 0; i < n; ++i) {
185194
this.s[i + 1] = this.s[i] + nums[i];
186195
}
@@ -202,34 +211,7 @@ class NumArray {
202211

203212
```rust
204213
struct NumArray {
205-
nums: Vec<i32>,
206-
}
207-
208-
/**
209-
* `&self` means the method takes an immutable reference.
210-
* If you need a mutable reference, change it to `&mut self` instead.
211-
*/
212-
impl NumArray {
213-
fn new(nums: Vec<i32>) -> Self {
214-
Self {
215-
nums,
216-
}
217-
}
218-
219-
fn sum_range(&self, left: i32, right: i32) -> i32 {
220-
let (left, right) = (left as usize, right as usize);
221-
self.nums[left..=right].iter().sum::<i32>()
222-
}
223-
}/**
224-
* Your NumArray object will be instantiated and called as such:
225-
* let obj = NumArray::new(nums);
226-
* let ret_1: i32 = obj.sum_range(left, right);
227-
*/
228-
```
229-
230-
```rust
231-
struct NumArray {
232-
sums: Vec<i32>,
214+
s: Vec<i32>,
233215
}
234216

235217
/**
@@ -239,15 +221,15 @@ struct NumArray {
239221
impl NumArray {
240222
fn new(mut nums: Vec<i32>) -> Self {
241223
let n = nums.len();
242-
let mut sums = vec![0; n + 1];
224+
let mut s = vec![0; n + 1];
243225
for i in 0..n {
244-
sums[i + 1] = sums[i] + nums[i];
226+
s[i + 1] = s[i] + nums[i];
245227
}
246-
Self { sums }
228+
Self { s }
247229
}
248230

249231
fn sum_range(&self, left: i32, right: i32) -> i32 {
250-
self.sums[(right + 1) as usize] - self.sums[left as usize]
232+
self.s[(right + 1) as usize] - self.s[left as usize]
251233
}
252234
}/**
253235
* Your NumArray object will be instantiated and called as such:
@@ -260,25 +242,26 @@ impl NumArray {
260242

261243
```c
262244
typedef struct {
263-
int* sums;
245+
int* s;
264246
} NumArray;
265247

266-
NumArray* numArrayCreate(int* nums, int numsSize) {
267-
int* sums = malloc(sizeof(int) * (numsSize + 1));
268-
memset(sums, 0, numsSize + 1);
269-
for (int i = 0; i < numsSize; i++) {
270-
sums[i + 1] = sums[i] + nums[i];
248+
NumArray* numArrayCreate(int* nums, int n) {
249+
int* s = malloc(sizeof(int) * (n + 1));
250+
s[0] = 0;
251+
for (int i = 0; i < n; i++) {
252+
s[i + 1] = s[i] + nums[i];
271253
}
272-
NumArray* res = malloc(sizeof(NumArray));
273-
res->sums = sums;
274-
return res;
254+
NumArray* obj = malloc(sizeof(NumArray));
255+
obj->s = s;
256+
return obj;
275257
}
276258

277259
int numArraySumRange(NumArray* obj, int left, int right) {
278-
return obj->sums[right + 1] - obj->sums[left];
260+
return obj->s[right + 1] - obj->s[left];
279261
}
280262

281263
void numArrayFree(NumArray* obj) {
264+
free(obj->s);
282265
free(obj);
283266
}
284267

@@ -299,20 +282,27 @@ class NumArray {
299282
* @param Integer[] $nums
300283
*/
301284
function __construct($nums) {
302-
$this->sum = [0];
303-
for ($i = 0; $i < count($nums); $i++) {
304-
array_push($this->sum, $this->sum[$i] + $nums[$i]);
285+
$this->s = [0];
286+
foreach ($nums as $x) {
287+
$this->s[] = $this->s[count($this->s) - 1] + $x;
305288
}
306289
}
290+
307291
/**
308292
* @param Integer $left
309293
* @param Integer $right
310294
* @return Integer
311295
*/
312296
function sumRange($left, $right) {
313-
return $this->sum[$right + 1] - $this->sum[$left];
297+
return $this->s[$right + 1] - $this->s[$left];
314298
}
315299
}
300+
301+
/**
302+
* Your NumArray object will be instantiated and called as such:
303+
* $obj = NumArray($nums);
304+
* $ret_1 = $obj->sumRange($left, $right);
305+
*/
316306
```
317307

318308
### **...**

0 commit comments

Comments
 (0)