File tree Expand file tree Collapse file tree 24 files changed +121
-544
lines changed
2678.Number of Senior Citizens
2682.Find the Losers of the Circular Game
2683.Neighboring Bitwise XOR
2716.Minimize String Length
2717.Semi-Ordered Permutation Expand file tree Collapse file tree 24 files changed +121
-544
lines changed Original file line number Diff line number Diff line change @@ -135,52 +135,7 @@ func countSeniors(details []string) (ans int) {
135
135
136
136
``` ts
137
137
function countSeniors(details : string []): number {
138
- let ans = 0 ;
139
- for (const x of details ) {
140
- const age = parseInt (x .slice (11 , 13 ));
141
- if (age > 60 ) {
142
- ++ ans ;
143
- }
144
- }
145
- return ans ;
146
- }
147
- ```
148
-
149
- #### Rust
150
-
151
- ``` rust
152
- impl Solution {
153
- pub fn count_seniors (details : Vec <String >) -> i32 {
154
- let mut ans = 0 ;
155
-
156
- for s in details . iter () {
157
- if let Ok (age ) = s [11 .. 13 ]. parse :: <i32 >() {
158
- if age > 60 {
159
- ans += 1 ;
160
- }
161
- }
162
- }
163
-
164
- ans
165
- }
166
- }
167
- ```
168
-
169
- <!-- tabs: end -->
170
-
171
- <!-- solution: end -->
172
-
173
- <!-- solution: start -->
174
-
175
- ### 方法二
176
-
177
- <!-- tabs: start -->
178
-
179
- #### TypeScript
180
-
181
- ``` ts
182
- function countSeniors(details : string []): number {
183
- return details .filter (v => parseInt (v .slice (11 , 13 )) > 60 ).length ;
138
+ return details .filter (x => + x .slice (11 , 13 ) > 60 ).length ;
184
139
}
185
140
```
186
141
Original file line number Diff line number Diff line change @@ -133,52 +133,7 @@ func countSeniors(details []string) (ans int) {
133
133
134
134
``` ts
135
135
function countSeniors(details : string []): number {
136
- let ans = 0 ;
137
- for (const x of details ) {
138
- const age = parseInt (x .slice (11 , 13 ));
139
- if (age > 60 ) {
140
- ++ ans ;
141
- }
142
- }
143
- return ans ;
144
- }
145
- ```
146
-
147
- #### Rust
148
-
149
- ``` rust
150
- impl Solution {
151
- pub fn count_seniors (details : Vec <String >) -> i32 {
152
- let mut ans = 0 ;
153
-
154
- for s in details . iter () {
155
- if let Ok (age ) = s [11 .. 13 ]. parse :: <i32 >() {
156
- if age > 60 {
157
- ans += 1 ;
158
- }
159
- }
160
- }
161
-
162
- ans
163
- }
164
- }
165
- ```
166
-
167
- <!-- tabs: end -->
168
-
169
- <!-- solution: end -->
170
-
171
- <!-- solution: start -->
172
-
173
- ### Solution 2
174
-
175
- <!-- tabs: start -->
176
-
177
- #### TypeScript
178
-
179
- ``` ts
180
- function countSeniors(details : string []): number {
181
- return details .filter (v => parseInt (v .slice (11 , 13 )) > 60 ).length ;
136
+ return details .filter (x => + x .slice (11 , 13 ) > 60 ).length ;
182
137
}
183
138
```
184
139
Original file line number Diff line number Diff line change 1
1
impl Solution {
2
2
pub fn count_seniors ( details : Vec < String > ) -> i32 {
3
- let mut ans = 0 ;
4
-
5
- for s in details. iter ( ) {
6
- if let Ok ( age) = s[ 11 ..13 ] . parse :: < i32 > ( ) {
7
- if age > 60 {
8
- ans += 1 ;
9
- }
10
- }
11
- }
12
-
13
- ans
3
+ details
4
+ . iter ( )
5
+ . filter_map ( |s| s[ 11 ..13 ] . parse :: < i32 > ( ) . ok ( ) )
6
+ . filter ( |& age| age > 60 )
7
+ . count ( ) as i32
14
8
}
15
9
}
Original file line number Diff line number Diff line change 1
1
function countSeniors ( details : string [ ] ) : number {
2
- let ans = 0 ;
3
- for ( const x of details ) {
4
- const age = parseInt ( x . slice ( 11 , 13 ) ) ;
5
- if ( age > 60 ) {
6
- ++ ans ;
7
- }
8
- }
9
- return ans ;
2
+ return details . filter ( x => + x . slice ( 11 , 13 ) > 60 ) . length ;
10
3
}
Load Diff This file was deleted.
Load Diff This file was deleted.
Original file line number Diff line number Diff line change @@ -168,22 +168,18 @@ function matrixSum(nums: number[][]): number {
168
168
169
169
``` rust
170
170
impl Solution {
171
- pub fn matrix_sum (nums : Vec <Vec <i32 >>) -> i32 {
172
- let mut nums = nums ;
173
- for row in nums . iter_mut () {
171
+ pub fn matrix_sum (mut nums : Vec <Vec <i32 >>) -> i32 {
172
+ for row in & mut nums {
174
173
row . sort ();
175
174
}
176
- let transposed : Vec <Vec <i32 >> = (0 .. nums [0 ]. len ())
177
- . map (| i | {
178
- nums . iter ()
179
- . map (| row | row [i ])
180
- . collect ()
181
- })
182
- . collect ();
183
-
184
- transposed
185
- . iter ()
186
- . map (| row | row . iter (). max (). unwrap ())
175
+ (0 .. nums [0 ]. len ())
176
+ . map (| col |
177
+ nums
178
+ . iter ()
179
+ . map (| row | row [col ])
180
+ . max ()
181
+ . unwrap ()
182
+ )
187
183
. sum ()
188
184
}
189
185
}
@@ -193,38 +189,4 @@ impl Solution {
193
189
194
190
<!-- solution: end -->
195
191
196
- <!-- solution: start -->
197
-
198
- ### 方法二
199
-
200
- <!-- tabs: start -->
201
-
202
- #### Rust
203
-
204
- ``` rust
205
- impl Solution {
206
- pub fn matrix_sum (nums : Vec <Vec <i32 >>) -> i32 {
207
- let mut nums = nums . clone ();
208
- for row in nums . iter_mut () {
209
- row . sort ();
210
- }
211
-
212
- let mut ans = 0 ;
213
- for j in 0 .. nums [0 ]. len () {
214
- let mut mx = 0 ;
215
- for row in & nums {
216
- mx = mx . max (row [j ]);
217
- }
218
- ans += mx ;
219
- }
220
-
221
- ans
222
- }
223
- }
224
- ```
225
-
226
- <!-- tabs: end -->
227
-
228
- <!-- solution: end -->
229
-
230
192
<!-- problem: end -->
Original file line number Diff line number Diff line change @@ -159,22 +159,18 @@ function matrixSum(nums: number[][]): number {
159
159
160
160
``` rust
161
161
impl Solution {
162
- pub fn matrix_sum (nums : Vec <Vec <i32 >>) -> i32 {
163
- let mut nums = nums ;
164
- for row in nums . iter_mut () {
162
+ pub fn matrix_sum (mut nums : Vec <Vec <i32 >>) -> i32 {
163
+ for row in & mut nums {
165
164
row . sort ();
166
165
}
167
- let transposed : Vec <Vec <i32 >> = (0 .. nums [0 ]. len ())
168
- . map (| i | {
169
- nums . iter ()
170
- . map (| row | row [i ])
171
- . collect ()
172
- })
173
- . collect ();
174
-
175
- transposed
176
- . iter ()
177
- . map (| row | row . iter (). max (). unwrap ())
166
+ (0 .. nums [0 ]. len ())
167
+ . map (| col |
168
+ nums
169
+ . iter ()
170
+ . map (| row | row [col ])
171
+ . max ()
172
+ . unwrap ()
173
+ )
178
174
. sum ()
179
175
}
180
176
}
@@ -184,38 +180,4 @@ impl Solution {
184
180
185
181
<!-- solution: end -->
186
182
187
- <!-- solution: start -->
188
-
189
- ### Solution 2
190
-
191
- <!-- tabs: start -->
192
-
193
- #### Rust
194
-
195
- ``` rust
196
- impl Solution {
197
- pub fn matrix_sum (nums : Vec <Vec <i32 >>) -> i32 {
198
- let mut nums = nums . clone ();
199
- for row in nums . iter_mut () {
200
- row . sort ();
201
- }
202
-
203
- let mut ans = 0 ;
204
- for j in 0 .. nums [0 ]. len () {
205
- let mut mx = 0 ;
206
- for row in & nums {
207
- mx = mx . max (row [j ]);
208
- }
209
- ans += mx ;
210
- }
211
-
212
- ans
213
- }
214
- }
215
- ```
216
-
217
- <!-- tabs: end -->
218
-
219
- <!-- solution: end -->
220
-
221
183
<!-- problem: end -->
Original file line number Diff line number Diff line change 1
1
impl Solution {
2
- pub fn matrix_sum ( nums : Vec < Vec < i32 > > ) -> i32 {
3
- let mut nums = nums;
4
- for row in nums. iter_mut ( ) {
2
+ pub fn matrix_sum ( mut nums : Vec < Vec < i32 > > ) -> i32 {
3
+ for row in & mut nums {
5
4
row. sort ( ) ;
6
5
}
7
- let transposed: Vec < Vec < i32 > > = ( 0 ..nums[ 0 ] . len ( ) )
8
- . map ( |i| {
9
- nums. iter ( )
10
- . map ( |row| row[ i] )
11
- . collect ( )
12
- } )
13
- . collect ( ) ;
14
-
15
- transposed
16
- . iter ( )
17
- . map ( |row| row. iter ( ) . max ( ) . unwrap ( ) )
6
+ ( 0 ..nums[ 0 ] . len ( ) )
7
+ . map ( |col|
8
+ nums
9
+ . iter ( )
10
+ . map ( |row| row[ col] )
11
+ . max ( )
12
+ . unwrap ( )
13
+ )
18
14
. sum ( )
19
15
}
20
16
}
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments