File tree Expand file tree Collapse file tree 4 files changed +119
-0
lines changed
solution/3300-3399/3354.Make Array Elements Equal to Zero Expand file tree Collapse file tree 4 files changed +119
-0
lines changed Original file line number Diff line number Diff line change @@ -216,6 +216,49 @@ function countValidSelections(nums: number[]): number {
216216}
217217```
218218
219+ #### Rust
220+
221+ ``` rust
222+ impl Solution {
223+ pub fn count_valid_selections (nums : Vec <i32 >) -> i32 {
224+ let s : i32 = nums . iter (). sum ();
225+ let mut ans = 0 ;
226+ let mut l = 0 ;
227+ for & x in & nums {
228+ if x != 0 {
229+ l += x ;
230+ } else if l * 2 == s {
231+ ans += 2 ;
232+ } else if (l * 2 - s ). abs () <= 1 {
233+ ans += 1 ;
234+ }
235+ }
236+ ans
237+ }
238+ }
239+ ```
240+
241+ #### C#
242+
243+ ``` cs
244+ public class Solution {
245+ public int CountValidSelections (int [] nums ) {
246+ int s = nums .Sum ();
247+ int ans = 0 , l = 0 ;
248+ foreach (int x in nums ) {
249+ if (x != 0 ) {
250+ l += x ;
251+ } else if (l * 2 == s ) {
252+ ans += 2 ;
253+ } else if (Math .Abs (l * 2 - s ) <= 1 ) {
254+ ans += 1 ;
255+ }
256+ }
257+ return ans ;
258+ }
259+ }
260+ ```
261+
219262<!-- tabs: end -->
220263
221264<!-- solution: end -->
Original file line number Diff line number Diff line change @@ -214,6 +214,49 @@ function countValidSelections(nums: number[]): number {
214214}
215215```
216216
217+ #### Rust
218+
219+ ``` rust
220+ impl Solution {
221+ pub fn count_valid_selections (nums : Vec <i32 >) -> i32 {
222+ let s : i32 = nums . iter (). sum ();
223+ let mut ans = 0 ;
224+ let mut l = 0 ;
225+ for & x in & nums {
226+ if x != 0 {
227+ l += x ;
228+ } else if l * 2 == s {
229+ ans += 2 ;
230+ } else if (l * 2 - s ). abs () <= 1 {
231+ ans += 1 ;
232+ }
233+ }
234+ ans
235+ }
236+ }
237+ ```
238+
239+ #### C#
240+
241+ ``` cs
242+ public class Solution {
243+ public int CountValidSelections (int [] nums ) {
244+ int s = nums .Sum ();
245+ int ans = 0 , l = 0 ;
246+ foreach (int x in nums ) {
247+ if (x != 0 ) {
248+ l += x ;
249+ } else if (l * 2 == s ) {
250+ ans += 2 ;
251+ } else if (Math .Abs (l * 2 - s ) <= 1 ) {
252+ ans += 1 ;
253+ }
254+ }
255+ return ans ;
256+ }
257+ }
258+ ```
259+
217260<!-- tabs: end -->
218261
219262<!-- solution: end -->
Original file line number Diff line number Diff line change 1+ public class Solution {
2+ public int CountValidSelections ( int [ ] nums ) {
3+ int s = nums . Sum ( ) ;
4+ int ans = 0 , l = 0 ;
5+ foreach ( int x in nums ) {
6+ if ( x != 0 ) {
7+ l += x ;
8+ } else if ( l * 2 == s ) {
9+ ans += 2 ;
10+ } else if ( Math . Abs ( l * 2 - s ) <= 1 ) {
11+ ans += 1 ;
12+ }
13+ }
14+ return ans ;
15+ }
16+ }
Original file line number Diff line number Diff line change 1+ impl Solution {
2+ pub fn count_valid_selections ( nums : Vec < i32 > ) -> i32 {
3+ let s: i32 = nums. iter ( ) . sum ( ) ;
4+ let mut ans = 0 ;
5+ let mut l = 0 ;
6+ for & x in & nums {
7+ if x != 0 {
8+ l += x;
9+ } else if l * 2 == s {
10+ ans += 2 ;
11+ } else if ( l * 2 - s) . abs ( ) <= 1 {
12+ ans += 1 ;
13+ }
14+ }
15+ ans
16+ }
17+ }
You can’t perform that action at this time.
0 commit comments