Skip to content

Commit 37f56a8

Browse files
committed
feat: add rust solution to lc problems: No.0303,1603
- No,0303.Range Sum Query - Immutable - No.1603.Design Parking System
1 parent c8fdac1 commit 37f56a8

File tree

6 files changed

+274
-0
lines changed

6 files changed

+274
-0
lines changed

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

+68
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,74 @@ class NumArray {
106106
*/
107107
```
108108

109+
### **Rust**
110+
111+
```rust
112+
struct NumArray {
113+
nums: Vec<i32>
114+
}
115+
116+
117+
/**
118+
* `&self` means the method takes an immutable reference.
119+
* If you need a mutable reference, change it to `&mut self` instead.
120+
*/
121+
impl NumArray {
122+
123+
fn new(nums: Vec<i32>) -> Self {
124+
Self {
125+
nums
126+
}
127+
}
128+
129+
fn sum_range(&self, left: i32, right: i32) -> i32 {
130+
let (left, right) = (left as usize, right as usize);
131+
self.nums[left..=right].iter().sum::<i32>()
132+
}
133+
}
134+
135+
/**
136+
* Your NumArray object will be instantiated and called as such:
137+
* let obj = NumArray::new(nums);
138+
* let ret_1: i32 = obj.sum_range(left, right);
139+
*/
140+
```
141+
142+
```rust
143+
struct NumArray {
144+
nums: Vec<i32>
145+
}
146+
147+
148+
/**
149+
* `&self` means the method takes an immutable reference.
150+
* If you need a mutable reference, change it to `&mut self` instead.
151+
*/
152+
impl NumArray {
153+
154+
fn new(mut nums: Vec<i32>) -> Self {
155+
let n = nums.len();
156+
for i in 1..n {
157+
nums[i] += nums[i - 1];
158+
}
159+
Self {
160+
nums
161+
}
162+
}
163+
164+
fn sum_range(&self, left: i32, right: i32) -> i32 {
165+
let (left, right) = (left as usize, right as usize);
166+
self.nums[right] - if left == 0 { 0 } else { self.nums[left - 1] }
167+
}
168+
}
169+
170+
/**
171+
* Your NumArray object will be instantiated and called as such:
172+
* let obj = NumArray::new(nums);
173+
* let ret_1: i32 = obj.sum_range(left, right);
174+
*/
175+
```
176+
109177
### **...**
110178

111179
```

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

+68
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,74 @@ class NumArray {
9696
*/
9797
```
9898

99+
### **Rust**
100+
101+
```rust
102+
struct NumArray {
103+
nums: Vec<i32>
104+
}
105+
106+
107+
/**
108+
* `&self` means the method takes an immutable reference.
109+
* If you need a mutable reference, change it to `&mut self` instead.
110+
*/
111+
impl NumArray {
112+
113+
fn new(nums: Vec<i32>) -> Self {
114+
Self {
115+
nums
116+
}
117+
}
118+
119+
fn sum_range(&self, left: i32, right: i32) -> i32 {
120+
let (left, right) = (left as usize, right as usize);
121+
self.nums[left..=right].iter().sum::<i32>()
122+
}
123+
}
124+
125+
/**
126+
* Your NumArray object will be instantiated and called as such:
127+
* let obj = NumArray::new(nums);
128+
* let ret_1: i32 = obj.sum_range(left, right);
129+
*/
130+
```
131+
132+
```rust
133+
struct NumArray {
134+
nums: Vec<i32>
135+
}
136+
137+
138+
/**
139+
* `&self` means the method takes an immutable reference.
140+
* If you need a mutable reference, change it to `&mut self` instead.
141+
*/
142+
impl NumArray {
143+
144+
fn new(mut nums: Vec<i32>) -> Self {
145+
let n = nums.len();
146+
for i in 1..n {
147+
nums[i] += nums[i - 1];
148+
}
149+
Self {
150+
nums
151+
}
152+
}
153+
154+
fn sum_range(&self, left: i32, right: i32) -> i32 {
155+
let (left, right) = (left as usize, right as usize);
156+
self.nums[right] - if left == 0 { 0 } else { self.nums[left - 1] }
157+
}
158+
}
159+
160+
/**
161+
* Your NumArray object will be instantiated and called as such:
162+
* let obj = NumArray::new(nums);
163+
* let ret_1: i32 = obj.sum_range(left, right);
164+
*/
165+
```
166+
99167
### **...**
100168

101169
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
struct NumArray {
2+
nums: Vec<i32>
3+
}
4+
5+
6+
/**
7+
* `&self` means the method takes an immutable reference.
8+
* If you need a mutable reference, change it to `&mut self` instead.
9+
*/
10+
impl NumArray {
11+
12+
fn new(mut nums: Vec<i32>) -> Self {
13+
let n = nums.len();
14+
for i in 1..n {
15+
nums[i] += nums[i - 1];
16+
}
17+
Self {
18+
nums
19+
}
20+
}
21+
22+
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] }
25+
}
26+
}
27+
28+
/**
29+
* Your NumArray object will be instantiated and called as such:
30+
* let obj = NumArray::new(nums);
31+
* let ret_1: i32 = obj.sum_range(left, right);
32+
*/

solution/1600-1699/1603.Design Parking System/README.md

+37
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,43 @@ class ParkingSystem {
106106
*/
107107
```
108108

109+
### **Rust**
110+
111+
```rust
112+
struct ParkingSystem {
113+
list: [i32; 3],
114+
}
115+
116+
117+
/**
118+
* `&self` means the method takes an immutable reference.
119+
* If you need a mutable reference, change it to `&mut self` instead.
120+
*/
121+
impl ParkingSystem {
122+
123+
fn new(big: i32, medium: i32, small: i32) -> Self {
124+
Self {
125+
list: [big, medium, small]
126+
}
127+
}
128+
129+
fn add_car(&mut self, car_type: i32) -> bool {
130+
let i = (car_type - 1) as usize;
131+
if self.list[i] == 0 {
132+
return false;
133+
}
134+
self.list[i] -= 1;
135+
true
136+
}
137+
}
138+
139+
/**
140+
* Your ParkingSystem object will be instantiated and called as such:
141+
* let obj = ParkingSystem::new(big, medium, small);
142+
* let ret_1: bool = obj.add_car(carType);
143+
*/
144+
```
145+
109146
### **...**
110147

111148
```

solution/1600-1699/1603.Design Parking System/README_EN.md

+37
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,43 @@ class ParkingSystem {
9494
*/
9595
```
9696

97+
### **Rust**
98+
99+
```rust
100+
struct ParkingSystem {
101+
list: [i32; 3],
102+
}
103+
104+
105+
/**
106+
* `&self` means the method takes an immutable reference.
107+
* If you need a mutable reference, change it to `&mut self` instead.
108+
*/
109+
impl ParkingSystem {
110+
111+
fn new(big: i32, medium: i32, small: i32) -> Self {
112+
Self {
113+
list: [big, medium, small]
114+
}
115+
}
116+
117+
fn add_car(&mut self, car_type: i32) -> bool {
118+
let i = (car_type - 1) as usize;
119+
if self.list[i] == 0 {
120+
return false;
121+
}
122+
self.list[i] -= 1;
123+
true
124+
}
125+
}
126+
127+
/**
128+
* Your ParkingSystem object will be instantiated and called as such:
129+
* let obj = ParkingSystem::new(big, medium, small);
130+
* let ret_1: bool = obj.add_car(carType);
131+
*/
132+
```
133+
97134
### **...**
98135

99136
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
struct ParkingSystem {
2+
list: [i32; 3],
3+
}
4+
5+
6+
/**
7+
* `&self` means the method takes an immutable reference.
8+
* If you need a mutable reference, change it to `&mut self` instead.
9+
*/
10+
impl ParkingSystem {
11+
12+
fn new(big: i32, medium: i32, small: i32) -> Self {
13+
Self {
14+
list: [big, medium, small]
15+
}
16+
}
17+
18+
fn add_car(&mut self, car_type: i32) -> bool {
19+
let i = (car_type - 1) as usize;
20+
if self.list[i] == 0 {
21+
return false;
22+
}
23+
self.list[i] -= 1;
24+
true
25+
}
26+
}
27+
28+
/**
29+
* Your ParkingSystem object will be instantiated and called as such:
30+
* let obj = ParkingSystem::new(big, medium, small);
31+
* let ret_1: bool = obj.add_car(carType);
32+
*/

0 commit comments

Comments
 (0)