From 3d7f1ba64e2adf02417ae51ca652b336245b755a Mon Sep 17 00:00:00 2001 From: yanglbme Date: Wed, 11 Sep 2024 19:52:48 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.2133 No.2133.Check if Every Row and Column Contains All Numbers --- .../README.md | 107 +++++++++--------- .../README_EN.md | 107 +++++++++--------- .../Solution.cpp | 25 ++-- .../Solution.go | 20 ++-- .../Solution.java | 21 ++-- .../Solution.py | 16 +-- .../Solution.ts | 25 ++-- 7 files changed, 157 insertions(+), 164 deletions(-) diff --git a/solution/2100-2199/2133.Check if Every Row and Column Contains All Numbers/README.md b/solution/2100-2199/2133.Check if Every Row and Column Contains All Numbers/README.md index a79a45b585b6b..68564eef9cae3 100644 --- a/solution/2100-2199/2133.Check if Every Row and Column Contains All Numbers/README.md +++ b/solution/2100-2199/2133.Check if Every Row and Column Contains All Numbers/README.md @@ -64,7 +64,11 @@ tags: -### 方法一 +### 方法一:哈希表 + +遍历矩阵的每一行和每一列,使用哈希表记录每个数字是否出现过,如果某一行或某一列中有数字重复出现,则返回 `false`,否则返回 `true`。 + +时间复杂度 $O(n^2)$,空间复杂度 $O(n)$。其中 $n$ 为矩阵的大小。 @@ -74,21 +78,7 @@ tags: class Solution: def checkValid(self, matrix: List[List[int]]) -> bool: n = len(matrix) - for i in range(n): - seen = [False] * n - for j in range(n): - v = matrix[i][j] - 1 - if seen[v]: - return False - seen[v] = True - for j in range(n): - seen = [False] * n - for i in range(n): - v = matrix[i][j] - 1 - if seen[v]: - return False - seen[v] = True - return True + return all(len(set(row)) == n for row in chain(matrix, zip(*matrix))) ``` #### Java @@ -97,24 +87,23 @@ class Solution: class Solution { public boolean checkValid(int[][] matrix) { int n = matrix.length; - for (int i = 0; i < n; ++i) { - boolean[] seen = new boolean[n]; - for (int j = 0; j < n; ++j) { - int v = matrix[i][j] - 1; - if (seen[v]) { + boolean[] vis = new boolean[n + 1]; + for (var row : matrix) { + Arrays.fill(vis, false); + for (int x : row) { + if (vis[x]) { return false; } - seen[v] = true; + vis[x] = true; } } for (int j = 0; j < n; ++j) { - boolean[] seen = new boolean[n]; + Arrays.fill(vis, false); for (int i = 0; i < n; ++i) { - int v = matrix[i][j] - 1; - if (seen[v]) { + if (vis[matrix[i][j]]) { return false; } - seen[v] = true; + vis[matrix[i][j]] = true; } } return true; @@ -129,20 +118,23 @@ class Solution { public: bool checkValid(vector>& matrix) { int n = matrix.size(); - for (int i = 0; i < n; ++i) { - vector seen(n); - for (int j = 0; j < n; ++j) { - int v = matrix[i][j] - 1; - if (seen[v]) return false; - seen[v] = true; + bool vis[n + 1]; + for (const auto& row : matrix) { + memset(vis, false, sizeof(vis)); + for (int x : row) { + if (vis[x]) { + return false; + } + vis[x] = true; } } for (int j = 0; j < n; ++j) { - vector seen(n); + memset(vis, false, sizeof(vis)); for (int i = 0; i < n; ++i) { - int v = matrix[i][j] - 1; - if (seen[v]) return false; - seen[v] = true; + if (vis[matrix[i][j]]) { + return false; + } + vis[matrix[i][j]] = true; } } return true; @@ -155,24 +147,22 @@ public: ```go func checkValid(matrix [][]int) bool { n := len(matrix) - for i := 0; i < n; i++ { - seen := make([]bool, n) - for j := 0; j < n; j++ { - v := matrix[i][j] - 1 - if seen[v] { + for _, row := range matrix { + vis := make([]bool, n+1) + for _, x := range row { + if vis[x] { return false } - seen[v] = true + vis[x] = true } } for j := 0; j < n; j++ { - seen := make([]bool, n) + vis := make([]bool, n+1) for i := 0; i < n; i++ { - v := matrix[i][j] - 1 - if seen[v] { + if vis[matrix[i][j]] { return false } - seen[v] = true + vis[matrix[i][j]] = true } } return true @@ -184,14 +174,23 @@ func checkValid(matrix [][]int) bool { ```ts function checkValid(matrix: number[][]): boolean { const n = matrix.length; - let rows = Array.from({ length: n }, () => new Array(n).fill(false)); - let cols = Array.from({ length: n }, () => new Array(n).fill(false)); - for (let i = 0; i < n; i++) { - for (let j = 0; j < n; j++) { - let cur = matrix[i][j]; - if (rows[i][cur] || cols[j][cur]) return false; - rows[i][cur] = true; - cols[j][cur] = true; + const vis: boolean[] = Array(n + 1).fill(false); + for (const row of matrix) { + vis.fill(false); + for (const x of row) { + if (vis[x]) { + return false; + } + vis[x] = true; + } + } + for (let j = 0; j < n; ++j) { + vis.fill(false); + for (let i = 0; i < n; ++i) { + if (vis[matrix[i][j]]) { + return false; + } + vis[matrix[i][j]] = true; } } return true; diff --git a/solution/2100-2199/2133.Check if Every Row and Column Contains All Numbers/README_EN.md b/solution/2100-2199/2133.Check if Every Row and Column Contains All Numbers/README_EN.md index c9a85c8de829d..09888f0119ec8 100644 --- a/solution/2100-2199/2133.Check if Every Row and Column Contains All Numbers/README_EN.md +++ b/solution/2100-2199/2133.Check if Every Row and Column Contains All Numbers/README_EN.md @@ -58,7 +58,11 @@ Hence, we return false. -### Solution 1 +### Solution 1: Hash Table + +Traverse each row and column of the matrix, using a hash table to record whether each number has appeared. If any number appears more than once in a row or column, return `false`; otherwise, return `true` + +The time complexity is $O(n^2)$, and the space complexity is $O(n)$. Here, $n$ is the size of the matrix. @@ -68,21 +72,7 @@ Hence, we return false. class Solution: def checkValid(self, matrix: List[List[int]]) -> bool: n = len(matrix) - for i in range(n): - seen = [False] * n - for j in range(n): - v = matrix[i][j] - 1 - if seen[v]: - return False - seen[v] = True - for j in range(n): - seen = [False] * n - for i in range(n): - v = matrix[i][j] - 1 - if seen[v]: - return False - seen[v] = True - return True + return all(len(set(row)) == n for row in chain(matrix, zip(*matrix))) ``` #### Java @@ -91,24 +81,23 @@ class Solution: class Solution { public boolean checkValid(int[][] matrix) { int n = matrix.length; - for (int i = 0; i < n; ++i) { - boolean[] seen = new boolean[n]; - for (int j = 0; j < n; ++j) { - int v = matrix[i][j] - 1; - if (seen[v]) { + boolean[] vis = new boolean[n + 1]; + for (var row : matrix) { + Arrays.fill(vis, false); + for (int x : row) { + if (vis[x]) { return false; } - seen[v] = true; + vis[x] = true; } } for (int j = 0; j < n; ++j) { - boolean[] seen = new boolean[n]; + Arrays.fill(vis, false); for (int i = 0; i < n; ++i) { - int v = matrix[i][j] - 1; - if (seen[v]) { + if (vis[matrix[i][j]]) { return false; } - seen[v] = true; + vis[matrix[i][j]] = true; } } return true; @@ -123,20 +112,23 @@ class Solution { public: bool checkValid(vector>& matrix) { int n = matrix.size(); - for (int i = 0; i < n; ++i) { - vector seen(n); - for (int j = 0; j < n; ++j) { - int v = matrix[i][j] - 1; - if (seen[v]) return false; - seen[v] = true; + bool vis[n + 1]; + for (const auto& row : matrix) { + memset(vis, false, sizeof(vis)); + for (int x : row) { + if (vis[x]) { + return false; + } + vis[x] = true; } } for (int j = 0; j < n; ++j) { - vector seen(n); + memset(vis, false, sizeof(vis)); for (int i = 0; i < n; ++i) { - int v = matrix[i][j] - 1; - if (seen[v]) return false; - seen[v] = true; + if (vis[matrix[i][j]]) { + return false; + } + vis[matrix[i][j]] = true; } } return true; @@ -149,24 +141,22 @@ public: ```go func checkValid(matrix [][]int) bool { n := len(matrix) - for i := 0; i < n; i++ { - seen := make([]bool, n) - for j := 0; j < n; j++ { - v := matrix[i][j] - 1 - if seen[v] { + for _, row := range matrix { + vis := make([]bool, n+1) + for _, x := range row { + if vis[x] { return false } - seen[v] = true + vis[x] = true } } for j := 0; j < n; j++ { - seen := make([]bool, n) + vis := make([]bool, n+1) for i := 0; i < n; i++ { - v := matrix[i][j] - 1 - if seen[v] { + if vis[matrix[i][j]] { return false } - seen[v] = true + vis[matrix[i][j]] = true } } return true @@ -178,14 +168,23 @@ func checkValid(matrix [][]int) bool { ```ts function checkValid(matrix: number[][]): boolean { const n = matrix.length; - let rows = Array.from({ length: n }, () => new Array(n).fill(false)); - let cols = Array.from({ length: n }, () => new Array(n).fill(false)); - for (let i = 0; i < n; i++) { - for (let j = 0; j < n; j++) { - let cur = matrix[i][j]; - if (rows[i][cur] || cols[j][cur]) return false; - rows[i][cur] = true; - cols[j][cur] = true; + const vis: boolean[] = Array(n + 1).fill(false); + for (const row of matrix) { + vis.fill(false); + for (const x of row) { + if (vis[x]) { + return false; + } + vis[x] = true; + } + } + for (let j = 0; j < n; ++j) { + vis.fill(false); + for (let i = 0; i < n; ++i) { + if (vis[matrix[i][j]]) { + return false; + } + vis[matrix[i][j]] = true; } } return true; diff --git a/solution/2100-2199/2133.Check if Every Row and Column Contains All Numbers/Solution.cpp b/solution/2100-2199/2133.Check if Every Row and Column Contains All Numbers/Solution.cpp index d8ff21fdd4663..631fafdf52375 100644 --- a/solution/2100-2199/2133.Check if Every Row and Column Contains All Numbers/Solution.cpp +++ b/solution/2100-2199/2133.Check if Every Row and Column Contains All Numbers/Solution.cpp @@ -2,22 +2,25 @@ class Solution { public: bool checkValid(vector>& matrix) { int n = matrix.size(); - for (int i = 0; i < n; ++i) { - vector seen(n); - for (int j = 0; j < n; ++j) { - int v = matrix[i][j] - 1; - if (seen[v]) return false; - seen[v] = true; + bool vis[n + 1]; + for (const auto& row : matrix) { + memset(vis, false, sizeof(vis)); + for (int x : row) { + if (vis[x]) { + return false; + } + vis[x] = true; } } for (int j = 0; j < n; ++j) { - vector seen(n); + memset(vis, false, sizeof(vis)); for (int i = 0; i < n; ++i) { - int v = matrix[i][j] - 1; - if (seen[v]) return false; - seen[v] = true; + if (vis[matrix[i][j]]) { + return false; + } + vis[matrix[i][j]] = true; } } return true; } -}; \ No newline at end of file +}; diff --git a/solution/2100-2199/2133.Check if Every Row and Column Contains All Numbers/Solution.go b/solution/2100-2199/2133.Check if Every Row and Column Contains All Numbers/Solution.go index 2c72be861b42e..ad7ceeb6f5da6 100644 --- a/solution/2100-2199/2133.Check if Every Row and Column Contains All Numbers/Solution.go +++ b/solution/2100-2199/2133.Check if Every Row and Column Contains All Numbers/Solution.go @@ -1,24 +1,22 @@ func checkValid(matrix [][]int) bool { n := len(matrix) - for i := 0; i < n; i++ { - seen := make([]bool, n) - for j := 0; j < n; j++ { - v := matrix[i][j] - 1 - if seen[v] { + for _, row := range matrix { + vis := make([]bool, n+1) + for _, x := range row { + if vis[x] { return false } - seen[v] = true + vis[x] = true } } for j := 0; j < n; j++ { - seen := make([]bool, n) + vis := make([]bool, n+1) for i := 0; i < n; i++ { - v := matrix[i][j] - 1 - if seen[v] { + if vis[matrix[i][j]] { return false } - seen[v] = true + vis[matrix[i][j]] = true } } return true -} \ No newline at end of file +} diff --git a/solution/2100-2199/2133.Check if Every Row and Column Contains All Numbers/Solution.java b/solution/2100-2199/2133.Check if Every Row and Column Contains All Numbers/Solution.java index c5281f3eaa902..8d5b1cce7d765 100644 --- a/solution/2100-2199/2133.Check if Every Row and Column Contains All Numbers/Solution.java +++ b/solution/2100-2199/2133.Check if Every Row and Column Contains All Numbers/Solution.java @@ -1,26 +1,25 @@ class Solution { public boolean checkValid(int[][] matrix) { int n = matrix.length; - for (int i = 0; i < n; ++i) { - boolean[] seen = new boolean[n]; - for (int j = 0; j < n; ++j) { - int v = matrix[i][j] - 1; - if (seen[v]) { + boolean[] vis = new boolean[n + 1]; + for (var row : matrix) { + Arrays.fill(vis, false); + for (int x : row) { + if (vis[x]) { return false; } - seen[v] = true; + vis[x] = true; } } for (int j = 0; j < n; ++j) { - boolean[] seen = new boolean[n]; + Arrays.fill(vis, false); for (int i = 0; i < n; ++i) { - int v = matrix[i][j] - 1; - if (seen[v]) { + if (vis[matrix[i][j]]) { return false; } - seen[v] = true; + vis[matrix[i][j]] = true; } } return true; } -} \ No newline at end of file +} diff --git a/solution/2100-2199/2133.Check if Every Row and Column Contains All Numbers/Solution.py b/solution/2100-2199/2133.Check if Every Row and Column Contains All Numbers/Solution.py index cd1b8212430f6..13c32aa24384c 100644 --- a/solution/2100-2199/2133.Check if Every Row and Column Contains All Numbers/Solution.py +++ b/solution/2100-2199/2133.Check if Every Row and Column Contains All Numbers/Solution.py @@ -1,18 +1,4 @@ class Solution: def checkValid(self, matrix: List[List[int]]) -> bool: n = len(matrix) - for i in range(n): - seen = [False] * n - for j in range(n): - v = matrix[i][j] - 1 - if seen[v]: - return False - seen[v] = True - for j in range(n): - seen = [False] * n - for i in range(n): - v = matrix[i][j] - 1 - if seen[v]: - return False - seen[v] = True - return True + return all(len(set(row)) == n for row in chain(matrix, zip(*matrix))) diff --git a/solution/2100-2199/2133.Check if Every Row and Column Contains All Numbers/Solution.ts b/solution/2100-2199/2133.Check if Every Row and Column Contains All Numbers/Solution.ts index 33f5defa36b64..7432a86062795 100644 --- a/solution/2100-2199/2133.Check if Every Row and Column Contains All Numbers/Solution.ts +++ b/solution/2100-2199/2133.Check if Every Row and Column Contains All Numbers/Solution.ts @@ -1,13 +1,22 @@ function checkValid(matrix: number[][]): boolean { const n = matrix.length; - let rows = Array.from({ length: n }, () => new Array(n).fill(false)); - let cols = Array.from({ length: n }, () => new Array(n).fill(false)); - for (let i = 0; i < n; i++) { - for (let j = 0; j < n; j++) { - let cur = matrix[i][j]; - if (rows[i][cur] || cols[j][cur]) return false; - rows[i][cur] = true; - cols[j][cur] = true; + const vis: boolean[] = Array(n + 1).fill(false); + for (const row of matrix) { + vis.fill(false); + for (const x of row) { + if (vis[x]) { + return false; + } + vis[x] = true; + } + } + for (let j = 0; j < n; ++j) { + vis.fill(false); + for (let i = 0; i < n; ++i) { + if (vis[matrix[i][j]]) { + return false; + } + vis[matrix[i][j]] = true; } } return true;