File tree 3 files changed +85
-0
lines changed
solution/1300-1399/1380.Lucky Numbers in a Matrix
3 files changed +85
-0
lines changed Original file line number Diff line number Diff line change @@ -202,6 +202,36 @@ function luckyNumbers(matrix: number[][]): number[] {
202
202
}
203
203
```
204
204
205
+ #### JavaScript
206
+
207
+ ``` js
208
+ /**
209
+ * @param {number[][]} matrix
210
+ * @return {number[]}
211
+ */
212
+ var luckyNumbers = function (matrix ) {
213
+ const m = matrix .length ;
214
+ const n = matrix[0 ].length ;
215
+ const rows = new Array (m).fill (1 << 30 );
216
+ const cols = new Array (n).fill (0 );
217
+ for (let i = 0 ; i < m; ++ i) {
218
+ for (let j = 0 ; j < n; j++ ) {
219
+ rows[i] = Math .min (rows[i], matrix[i][j]);
220
+ cols[j] = Math .max (cols[j], matrix[i][j]);
221
+ }
222
+ }
223
+ const ans = [];
224
+ for (let i = 0 ; i < m; ++ i) {
225
+ for (let j = 0 ; j < n; j++ ) {
226
+ if (rows[i] === cols[j]) {
227
+ ans .push (rows[i]);
228
+ }
229
+ }
230
+ }
231
+ return ans;
232
+ };
233
+ ```
234
+
205
235
#### Rust
206
236
207
237
``` rust
Original file line number Diff line number Diff line change @@ -195,6 +195,36 @@ function luckyNumbers(matrix: number[][]): number[] {
195
195
}
196
196
```
197
197
198
+ #### JavaScript
199
+
200
+ ``` js
201
+ /**
202
+ * @param {number[][]} matrix
203
+ * @return {number[]}
204
+ */
205
+ var luckyNumbers = function (matrix ) {
206
+ const m = matrix .length ;
207
+ const n = matrix[0 ].length ;
208
+ const rows = new Array (m).fill (1 << 30 );
209
+ const cols = new Array (n).fill (0 );
210
+ for (let i = 0 ; i < m; ++ i) {
211
+ for (let j = 0 ; j < n; j++ ) {
212
+ rows[i] = Math .min (rows[i], matrix[i][j]);
213
+ cols[j] = Math .max (cols[j], matrix[i][j]);
214
+ }
215
+ }
216
+ const ans = [];
217
+ for (let i = 0 ; i < m; ++ i) {
218
+ for (let j = 0 ; j < n; j++ ) {
219
+ if (rows[i] === cols[j]) {
220
+ ans .push (rows[i]);
221
+ }
222
+ }
223
+ }
224
+ return ans;
225
+ };
226
+ ```
227
+
198
228
#### Rust
199
229
200
230
``` rust
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[][] } matrix
3
+ * @return {number[] }
4
+ */
5
+ var luckyNumbers = function ( matrix ) {
6
+ const m = matrix . length ;
7
+ const n = matrix [ 0 ] . length ;
8
+ const rows = new Array ( m ) . fill ( 1 << 30 ) ;
9
+ const cols = new Array ( n ) . fill ( 0 ) ;
10
+ for ( let i = 0 ; i < m ; ++ i ) {
11
+ for ( let j = 0 ; j < n ; j ++ ) {
12
+ rows [ i ] = Math . min ( rows [ i ] , matrix [ i ] [ j ] ) ;
13
+ cols [ j ] = Math . max ( cols [ j ] , matrix [ i ] [ j ] ) ;
14
+ }
15
+ }
16
+ const ans = [ ] ;
17
+ for ( let i = 0 ; i < m ; ++ i ) {
18
+ for ( let j = 0 ; j < n ; j ++ ) {
19
+ if ( rows [ i ] === cols [ j ] ) {
20
+ ans . push ( rows [ i ] ) ;
21
+ }
22
+ }
23
+ }
24
+ return ans ;
25
+ } ;
You can’t perform that action at this time.
0 commit comments