File tree Expand file tree Collapse file tree 2 files changed +86
-0
lines changed
solution/2500-2599/2574.Left and Right Sum Differences Expand file tree Collapse file tree 2 files changed +86
-0
lines changed Original file line number Diff line number Diff line change @@ -196,6 +196,49 @@ impl Solution {
196
196
}
197
197
```
198
198
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
+
199
242
### ** C**
200
243
201
244
``` c
Original file line number Diff line number Diff line change @@ -173,6 +173,49 @@ impl Solution {
173
173
}
174
174
```
175
175
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
+
176
219
### ** C**
177
220
178
221
``` c
You can’t perform that action at this time.
0 commit comments