Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 382cda4

Browse files
authored
feat: add rust solution to lc problem: No.2574 (doocs#1203)
1 parent 692a7e4 commit 382cda4

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

‎solution/2500-2599/2574.Left and Right Sum Differences/README.md‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,49 @@ impl Solution {
196196
}
197197
```
198198

199+
```rust
200+
impl Solution {
201+
pub fn left_right_difference(nums: Vec<i32>) -> Vec<i32> {
202+
let mut ans = vec![];
203+
204+
for i in 0..nums.len() {
205+
206+
let mut left = 0;
207+
for j in 0..i {
208+
left += nums[j];
209+
}
210+
211+
let mut right = 0;
212+
for k in i + 1..nums.len() {
213+
right += nums[k];
214+
}
215+
216+
ans.push((left - right).abs());
217+
}
218+
219+
ans
220+
}
221+
}
222+
```
223+
224+
```rust
225+
impl Solution {
226+
pub fn left_right_difference(nums: Vec<i32>) -> Vec<i32> {
227+
let mut left = 0;
228+
let mut right: i32 = nums.iter().sum();
229+
let mut ans = vec![];
230+
231+
for &x in &nums {
232+
right -= x;
233+
ans.push((left - right).abs());
234+
left += x;
235+
}
236+
237+
ans
238+
}
239+
}
240+
```
241+
199242
### **C**
200243

201244
```c

‎solution/2500-2599/2574.Left and Right Sum Differences/README_EN.md‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,49 @@ impl Solution {
173173
}
174174
```
175175

176+
```rust
177+
impl Solution {
178+
pub fn left_right_difference(nums: Vec<i32>) -> Vec<i32> {
179+
let mut ans = vec![];
180+
181+
for i in 0..nums.len() {
182+
183+
let mut left = 0;
184+
for j in 0..i {
185+
left += nums[j];
186+
}
187+
188+
let mut right = 0;
189+
for k in i + 1..nums.len() {
190+
right += nums[k];
191+
}
192+
193+
ans.push((left - right).abs());
194+
}
195+
196+
ans
197+
}
198+
}
199+
```
200+
201+
```rust
202+
impl Solution {
203+
pub fn left_right_difference(nums: Vec<i32>) -> Vec<i32> {
204+
let mut left = 0;
205+
let mut right: i32 = nums.iter().sum();
206+
let mut ans = vec![];
207+
208+
for &x in &nums {
209+
right -= x;
210+
ans.push((left - right).abs());
211+
left += x;
212+
}
213+
214+
ans
215+
}
216+
}
217+
```
218+
176219
### **C**
177220

178221
```c

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /