|
44 | 44 |
|
45 | 45 | ## Solutions
|
46 | 46 |
|
| 47 | +**Approach 1: Array Mark** |
| 48 | + |
| 49 | +We use arrays `rows` and `cols` to mark the rows and columns to be cleared. |
| 50 | + |
| 51 | +Then traverse the matrix again, and clear the elements in the rows and columns marked in `rows` and `cols`. |
| 52 | + |
| 53 | +The time complexity is $O(m\times n)$, and the space complexity is $O(m+n)$. Where $m$ and $n$ are the number of rows and columns of the matrix respectively. |
| 54 | + |
| 55 | +**Approach 2: Mark in Place** |
| 56 | + |
| 57 | +In the first method, we use an additional array to mark the rows and columns to be cleared. In fact, we can also use the first row and first column of the matrix to mark them, without creating an additional array. |
| 58 | + |
| 59 | +Since the first row and the first column are used to mark, their values may change due to the mark, so we need additional variables $i0$, $j0$ to mark whether the first row and the first column need to be cleared. |
| 60 | + |
| 61 | +The time complexity is $O(m\times n)$, and the space complexity is $O(1)$. Where $m$ and $n$ are the number of rows and columns of the matrix respectively. |
| 62 | + |
47 | 63 | <!-- tabs:start -->
|
48 | 64 |
|
49 | 65 | ### **Python3**
|
@@ -376,6 +392,136 @@ var setZeroes = function (matrix) {
|
376 | 392 | };
|
377 | 393 | ```
|
378 | 394 |
|
| 395 | +### **TypeScript** |
| 396 | + |
| 397 | +```ts |
| 398 | +/** |
| 399 | + Do not return anything, modify matrix in-place instead. |
| 400 | + */ |
| 401 | +function setZeroes(matrix: number[][]): void { |
| 402 | + const m = matrix.length; |
| 403 | + const n = matrix[0].length; |
| 404 | + const rows: boolean[] = new Array(m).fill(false); |
| 405 | + const cols: boolean[] = new Array(n).fill(false); |
| 406 | + for (let i = 0; i < m; ++i) { |
| 407 | + for (let j = 0; j < n; ++j) { |
| 408 | + if (matrix[i][j] === 0) { |
| 409 | + rows[i] = true; |
| 410 | + cols[j] = true; |
| 411 | + } |
| 412 | + } |
| 413 | + } |
| 414 | + for (let i = 0; i < m; ++i) { |
| 415 | + for (let j = 0; j < n; ++j) { |
| 416 | + if (rows[i] || cols[j]) { |
| 417 | + matrix[i][j] = 0; |
| 418 | + } |
| 419 | + } |
| 420 | + } |
| 421 | +} |
| 422 | +``` |
| 423 | + |
| 424 | +```ts |
| 425 | +/** |
| 426 | + Do not return anything, modify matrix in-place instead. |
| 427 | + */ |
| 428 | +function setZeroes(matrix: number[][]): void { |
| 429 | + const m = matrix.length; |
| 430 | + const n = matrix[0].length; |
| 431 | + const i0 = matrix[0].includes(0); |
| 432 | + const j0 = matrix.map(row => row[0]).includes(0); |
| 433 | + for (let i = 1; i < m; ++i) { |
| 434 | + for (let j = 1; j < n; ++j) { |
| 435 | + if (matrix[i][j] === 0) { |
| 436 | + matrix[i][0] = 0; |
| 437 | + matrix[0][j] = 0; |
| 438 | + } |
| 439 | + } |
| 440 | + } |
| 441 | + for (let i = 1; i < m; ++i) { |
| 442 | + for (let j = 1; j < n; ++j) { |
| 443 | + if (matrix[i][0] === 0 || matrix[0][j] === 0) { |
| 444 | + matrix[i][j] = 0; |
| 445 | + } |
| 446 | + } |
| 447 | + } |
| 448 | + if (i0) { |
| 449 | + matrix[0].fill(0); |
| 450 | + } |
| 451 | + if (j0) { |
| 452 | + for (let i = 0; i < m; ++i) { |
| 453 | + matrix[i][0] = 0; |
| 454 | + } |
| 455 | + } |
| 456 | +} |
| 457 | +``` |
| 458 | + |
| 459 | +### **C#** |
| 460 | + |
| 461 | +```cs |
| 462 | +public class Solution { |
| 463 | + public void SetZeroes(int[][] matrix) { |
| 464 | + int m = matrix.Length, n = matrix[0].Length; |
| 465 | + bool[] rows = new bool[m], cols = new bool[n]; |
| 466 | + for (int i = 0; i < m; ++i) { |
| 467 | + for (int j = 0; j < n; ++j) { |
| 468 | + if (matrix[i][j] == 0) { |
| 469 | + rows[i] = true; |
| 470 | + cols[j] = true; |
| 471 | + } |
| 472 | + } |
| 473 | + } |
| 474 | + for (int i = 0; i < m; ++i) { |
| 475 | + for (int j = 0; j < n; ++j) { |
| 476 | + if (rows[i] || cols[j]) { |
| 477 | + matrix[i][j] = 0; |
| 478 | + } |
| 479 | + } |
| 480 | + } |
| 481 | + } |
| 482 | +} |
| 483 | +``` |
| 484 | + |
| 485 | +```cs |
| 486 | +public class Solution { |
| 487 | + public void SetZeroes(int[][] matrix) { |
| 488 | + int m = matrix.Length, n = matrix[0].Length; |
| 489 | + bool i0 = matrix[0].Contains(0), j0 = false; |
| 490 | + for (int i = 0; i < m; ++i) { |
| 491 | + if (matrix[i][0] == 0) { |
| 492 | + j0 = true; |
| 493 | + break; |
| 494 | + } |
| 495 | + } |
| 496 | + for (int i = 1; i < m; ++i) { |
| 497 | + for (int j = 1; j < n; ++j) { |
| 498 | + if (matrix[i][j] == 0) { |
| 499 | + matrix[i][0] = 0; |
| 500 | + matrix[0][j] = 0; |
| 501 | + } |
| 502 | + } |
| 503 | + } |
| 504 | + for (int i = 1; i < m; ++i) { |
| 505 | + for (int j = 1; j < n; ++j) { |
| 506 | + if (matrix[i][0] == 0 || matrix[0][j] == 0) { |
| 507 | + matrix[i][j] = 0; |
| 508 | + } |
| 509 | + } |
| 510 | + } |
| 511 | + if (i0) { |
| 512 | + for (int j = 0; j < n; ++j) { |
| 513 | + matrix[0][j] = 0; |
| 514 | + } |
| 515 | + } |
| 516 | + if (j0) { |
| 517 | + for (int i = 0; i < m; ++i) { |
| 518 | + matrix[i][0] = 0; |
| 519 | + } |
| 520 | + } |
| 521 | + } |
| 522 | +} |
| 523 | +``` |
| 524 | + |
379 | 525 | ### **...**
|
380 | 526 |
|
381 | 527 | ```
|
|
0 commit comments