Skip to content

Commit 419c159

Browse files
committed
feat: add solutions to lc problems: No.0303, 1603
- No.0303.Range Sum Query - Immutable - No.1603.Design Parking System
1 parent a18790a commit 419c159

File tree

9 files changed

+365
-84
lines changed

9 files changed

+365
-84
lines changed

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

Lines changed: 65 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,36 @@ func (this *NumArray) SumRange(left int, right int) int {
158158
*/
159159
```
160160

161+
### **JavaScript**
162+
163+
```js
164+
/**
165+
* @param {number[]} nums
166+
*/
167+
var NumArray = function (nums) {
168+
const n = nums.length;
169+
this.s = new Array(n + 1).fill(0);
170+
for (let i = 0; i < n; ++i) {
171+
this.s[i + 1] = this.s[i] + nums[i];
172+
}
173+
};
174+
175+
/**
176+
* @param {number} left
177+
* @param {number} right
178+
* @return {number}
179+
*/
180+
NumArray.prototype.sumRange = function (left, right) {
181+
return this.s[right + 1] - this.s[left];
182+
};
183+
184+
/**
185+
* Your NumArray object will be instantiated and called as such:
186+
* var obj = new NumArray(nums)
187+
* var param_1 = obj.sumRange(left,right)
188+
*/
189+
```
190+
161191
### **TypeScript**
162192

163193
```ts
@@ -219,7 +249,7 @@ impl NumArray {
219249

220250
```rust
221251
struct NumArray {
222-
nums: Vec<i32>
252+
sums: Vec<i32>,
223253
}
224254

225255

@@ -231,17 +261,15 @@ impl NumArray {
231261

232262
fn new(mut nums: Vec<i32>) -> Self {
233263
let n = nums.len();
234-
for i in 1..n {
235-
nums[i] += nums[i - 1];
236-
}
237-
Self {
238-
nums
264+
let mut sums = vec![0; n + 1];
265+
for i in 0..n {
266+
sums[i + 1] = sums[i] + nums[i];
239267
}
268+
Self { sums }
240269
}
241270

242271
fn sum_range(&self, left: i32, right: i32) -> i32 {
243-
let (left, right) = (left as usize, right as usize);
244-
self.nums[right] - if left == 0 { 0 } else { self.nums[left - 1] }
272+
self.sums[(right + 1) as usize] - self.sums[left as usize]
245273
}
246274
}
247275

@@ -252,34 +280,40 @@ impl NumArray {
252280
*/
253281
```
254282

255-
### **JavaScript**
283+
### **C**
256284

257-
```js
258-
/**
259-
* @param {number[]} nums
260-
*/
261-
var NumArray = function (nums) {
262-
const n = nums.length;
263-
this.s = new Array(n + 1).fill(0);
264-
for (let i = 0; i < n; ++i) {
265-
this.s[i + 1] = this.s[i] + nums[i];
285+
```c
286+
typedef struct {
287+
int *sums;
288+
} NumArray;
289+
290+
291+
NumArray *numArrayCreate(int *nums, int numsSize) {
292+
int *sums = malloc(sizeof(int) * (numsSize + 1));
293+
memset(sums, 0, numsSize + 1);
294+
for (int i = 0; i < numsSize; i++) {
295+
sums[i + 1] = sums[i] + nums[i];
266296
}
267-
};
297+
NumArray *res = malloc(sizeof(NumArray));
298+
res->sums = sums;
299+
return res;
300+
}
268301

269-
/**
270-
* @param {number} left
271-
* @param {number} right
272-
* @return {number}
273-
*/
274-
NumArray.prototype.sumRange = function (left, right) {
275-
return this.s[right + 1] - this.s[left];
276-
};
302+
int numArraySumRange(NumArray *obj, int left, int right) {
303+
return obj->sums[right + 1] - obj->sums[left];
304+
}
305+
306+
void numArrayFree(NumArray *obj) {
307+
free(obj);
308+
}
277309

278310
/**
279-
* Your NumArray object will be instantiated and called as such:
280-
* var obj = new NumArray(nums)
281-
* var param_1 = obj.sumRange(left,right)
282-
*/
311+
* Your NumArray struct will be instantiated and called as such:
312+
* NumArray* obj = numArrayCreate(nums, numsSize);
313+
* int param_1 = numArraySumRange(obj, left, right);
314+
315+
* numArrayFree(obj);
316+
*/
283317
```
284318
285319
### **...**

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

Lines changed: 65 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,36 @@ func (this *NumArray) SumRange(left int, right int) int {
142142
*/
143143
```
144144

145+
### **JavaScript**
146+
147+
```js
148+
/**
149+
* @param {number[]} nums
150+
*/
151+
var NumArray = function (nums) {
152+
const n = nums.length;
153+
this.s = new Array(n + 1).fill(0);
154+
for (let i = 0; i < n; ++i) {
155+
this.s[i + 1] = this.s[i] + nums[i];
156+
}
157+
};
158+
159+
/**
160+
* @param {number} left
161+
* @param {number} right
162+
* @return {number}
163+
*/
164+
NumArray.prototype.sumRange = function (left, right) {
165+
return this.s[right + 1] - this.s[left];
166+
};
167+
168+
/**
169+
* Your NumArray object will be instantiated and called as such:
170+
* var obj = new NumArray(nums)
171+
* var param_1 = obj.sumRange(left,right)
172+
*/
173+
```
174+
145175
### **TypeScript**
146176

147177
```ts
@@ -203,7 +233,7 @@ impl NumArray {
203233

204234
```rust
205235
struct NumArray {
206-
nums: Vec<i32>
236+
sums: Vec<i32>,
207237
}
208238

209239

@@ -215,17 +245,15 @@ impl NumArray {
215245

216246
fn new(mut nums: Vec<i32>) -> Self {
217247
let n = nums.len();
218-
for i in 1..n {
219-
nums[i] += nums[i - 1];
220-
}
221-
Self {
222-
nums
248+
let mut sums = vec![0; n + 1];
249+
for i in 0..n {
250+
sums[i + 1] = sums[i] + nums[i];
223251
}
252+
Self { sums }
224253
}
225254

226255
fn sum_range(&self, left: i32, right: i32) -> i32 {
227-
let (left, right) = (left as usize, right as usize);
228-
self.nums[right] - if left == 0 { 0 } else { self.nums[left - 1] }
256+
self.sums[(right + 1) as usize] - self.sums[left as usize]
229257
}
230258
}
231259

@@ -236,34 +264,40 @@ impl NumArray {
236264
*/
237265
```
238266

239-
### **JavaScript**
267+
### **C**
240268

241-
```js
242-
/**
243-
* @param {number[]} nums
244-
*/
245-
var NumArray = function (nums) {
246-
const n = nums.length;
247-
this.s = new Array(n + 1).fill(0);
248-
for (let i = 0; i < n; ++i) {
249-
this.s[i + 1] = this.s[i] + nums[i];
269+
```c
270+
typedef struct {
271+
int *sums;
272+
} NumArray;
273+
274+
275+
NumArray *numArrayCreate(int *nums, int numsSize) {
276+
int *sums = malloc(sizeof(int) * (numsSize + 1));
277+
memset(sums, 0, numsSize + 1);
278+
for (int i = 0; i < numsSize; i++) {
279+
sums[i + 1] = sums[i] + nums[i];
250280
}
251-
};
281+
NumArray *res = malloc(sizeof(NumArray));
282+
res->sums = sums;
283+
return res;
284+
}
252285

253-
/**
254-
* @param {number} left
255-
* @param {number} right
256-
* @return {number}
257-
*/
258-
NumArray.prototype.sumRange = function (left, right) {
259-
return this.s[right + 1] - this.s[left];
260-
};
286+
int numArraySumRange(NumArray *obj, int left, int right) {
287+
return obj->sums[right + 1] - obj->sums[left];
288+
}
289+
290+
void numArrayFree(NumArray *obj) {
291+
free(obj);
292+
}
261293

262294
/**
263-
* Your NumArray object will be instantiated and called as such:
264-
* var obj = new NumArray(nums)
265-
* var param_1 = obj.sumRange(left,right)
266-
*/
295+
* Your NumArray struct will be instantiated and called as such:
296+
* NumArray* obj = numArrayCreate(nums, numsSize);
297+
* int param_1 = numArraySumRange(obj, left, right);
298+
299+
* numArrayFree(obj);
300+
*/
267301
```
268302
269303
### **...**
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
typedef struct {
2+
int *sums;
3+
} NumArray;
4+
5+
6+
NumArray *numArrayCreate(int *nums, int numsSize) {
7+
int *sums = malloc(sizeof(int) * (numsSize + 1));
8+
memset(sums, 0, numsSize + 1);
9+
for (int i = 0; i < numsSize; i++) {
10+
sums[i + 1] = sums[i] + nums[i];
11+
}
12+
NumArray *res = malloc(sizeof(NumArray));
13+
res->sums = sums;
14+
return res;
15+
}
16+
17+
int numArraySumRange(NumArray *obj, int left, int right) {
18+
return obj->sums[right + 1] - obj->sums[left];
19+
}
20+
21+
void numArrayFree(NumArray *obj) {
22+
free(obj);
23+
}
24+
25+
/**
26+
* Your NumArray struct will be instantiated and called as such:
27+
* NumArray* obj = numArrayCreate(nums, numsSize);
28+
* int param_1 = numArraySumRange(obj, left, right);
29+
30+
* numArrayFree(obj);
31+
*/
Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
struct NumArray {
2-
nums: Vec<i32>
2+
sums: Vec<i32>,
33
}
44

55

@@ -8,25 +8,22 @@ struct NumArray {
88
* If you need a mutable reference, change it to `&mut self` instead.
99
*/
1010
impl NumArray {
11-
1211
fn new(mut nums: Vec<i32>) -> Self {
1312
let n = nums.len();
14-
for i in 1..n {
15-
nums[i] += nums[i - 1];
16-
}
17-
Self {
18-
nums
13+
let mut sums = vec![0; n + 1];
14+
for i in 0..n {
15+
sums[i + 1] = sums[i] + nums[i];
1916
}
17+
Self { sums }
2018
}
2119

2220
fn sum_range(&self, left: i32, right: i32) -> i32 {
23-
let (left, right) = (left as usize, right as usize);
24-
self.nums[right] - if left == 0 { 0 } else { self.nums[left - 1] }
21+
self.sums[(right + 1) as usize] - self.sums[left as usize]
2522
}
2623
}
2724

2825
/**
2926
* Your NumArray object will be instantiated and called as such:
3027
* let obj = NumArray::new(nums);
3128
* let ret_1: i32 = obj.sum_range(left, right);
32-
*/
29+
*/

0 commit comments

Comments
 (0)