Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.2133 (#3514)
Browse files Browse the repository at this point in the history
No.2133.Check if Every Row and Column Contains All Numbers
  • Loading branch information
yanglbme authored Sep 12, 2024
1 parent 6b5f6b8 commit 718bb40
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 164 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:哈希表

遍历矩阵的每一行和每一列,使用哈希表记录每个数字是否出现过,如果某一行或某一列中有数字重复出现,则返回 `false`,否则返回 `true`

时间复杂度 $O(n^2)$,空间复杂度 $O(n)$。其中 $n$ 为矩阵的大小。

<!-- tabs:start -->

Expand All @@ -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
Expand All @@ -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;
Expand All @@ -129,20 +118,23 @@ class Solution {
public:
bool checkValid(vector<vector<int>>& matrix) {
int n = matrix.size();
for (int i = 0; i < n; ++i) {
vector<bool> 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<bool> 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;
Expand All @@ -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
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ Hence, we return false.

<!-- solution:start -->

### 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.

<!-- tabs:start -->

Expand All @@ -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
Expand All @@ -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;
Expand All @@ -123,20 +112,23 @@ class Solution {
public:
bool checkValid(vector<vector<int>>& matrix) {
int n = matrix.size();
for (int i = 0; i < n; ++i) {
vector<bool> 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<bool> 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;
Expand All @@ -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
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,25 @@ class Solution {
public:
bool checkValid(vector<vector<int>>& matrix) {
int n = matrix.size();
for (int i = 0; i < n; ++i) {
vector<bool> 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<bool> 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;
}
};
};
Loading

0 comments on commit 718bb40

Please sign in to comment.