Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

feat: add solutions to lc problems: No.2965~2968 #2112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 104 additions & 3 deletions solution/2900-2999/2965.Find Missing and Repeated Values/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,34 +46,135 @@

<!-- 这里可写通用的实现逻辑 -->

**方法一:计数**

我们创建一个长度为 $n^2 + 1$ 的数组 $cnt$,统计矩阵中每个数字出现的次数。

接下来遍历 $i \in [1, n^2]$,如果 $cnt[i] = 2$,则 $i$ 是重复的数字,我们将答案的第一个元素设为 $i$;如果 $cnt[i] = 0$,则 $i$ 是缺失的数字,我们将答案的第二个元素设为 $i$。

时间复杂度 $O(n^2)$,空间复杂度 $O(n^2)$。其中 $n$ 是矩阵的边长。

<!-- tabs:start -->

### **Python3**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```python

class Solution:
def findMissingAndRepeatedValues(self, grid: List[List[int]]) -> List[int]:
n = len(grid)
cnt = [0] * (n * n + 1)
for row in grid:
for v in row:
cnt[v] += 1
ans = [0] * 2
for i in range(1, n * n + 1):
if cnt[i] == 2:
ans[0] = i
if cnt[i] == 0:
ans[1] = i
return ans
```

### **Java**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```java

class Solution {
public int[] findMissingAndRepeatedValues(int[][] grid) {
int n = grid.length;
int[] cnt = new int[n * n + 1];
int[] ans = new int[2];
for (int[] row : grid) {
for (int x : row) {
if (++cnt[x] == 2) {
ans[0] = x;
}
}
}
for (int x = 1;; ++x) {
if (cnt[x] == 0) {
ans[1] = x;
return ans;
}
}
}
}
```

### **C++**

```cpp

class Solution {
public:
vector<int> findMissingAndRepeatedValues(vector<vector<int>>& grid) {
int n = grid.size();
vector<int> cnt(n * n + 1);
vector<int> ans(2);
for (auto& row : grid) {
for (int x : row) {
if (++cnt[x] == 2) {
ans[0] = x;
}
}
}
for (int x = 1;; ++x) {
if (cnt[x] == 0) {
ans[1] = x;
return ans;
}
}
}
};
```

### **Go**

```go
func findMissingAndRepeatedValues(grid [][]int) []int {
n := len(grid)
ans := make([]int, 2)
cnt := make([]int, n*n+1)
for _, row := range grid {
for _, x := range row {
cnt[x]++
if cnt[x] == 2 {
ans[0] = x
}
}
}
for x := 1; ; x++ {
if cnt[x] == 0 {
ans[1] = x
return ans
}
}
}
```

### **TypeScript**

```ts
function findMissingAndRepeatedValues(grid: number[][]): number[] {
const n = grid.length;
const cnt: number[] = Array(n * n + 1).fill(0);
const ans: number[] = Array(2).fill(0);
for (const row of grid) {
for (const x of row) {
if (++cnt[x] === 2) {
ans[0] = x;
}
}
}
for (let x = 1; ; ++x) {
if (cnt[x] === 0) {
ans[1] = x;
return ans;
}
}
}
```

### **...**
Expand Down
107 changes: 104 additions & 3 deletions solution/2900-2999/2965.Find Missing and Repeated Values/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,30 +38,131 @@

## Solutions

**Solution 1: Counting**

We create an array $cnt$ of length $n^2 + 1$ to count the frequency of each number in the matrix.

Next, we traverse $i \in [1, n^2]$. If $cnt[i] = 2$, then $i$ is the duplicated number, and we set the first element of the answer to $i$. If $cnt[i] = 0$, then $i$ is the missing number, and we set the second element of the answer to $i$.

The time complexity is $O(n^2)$, and the space complexity is $O(n^2)$. Here, $n$ is the side length of the matrix.

<!-- tabs:start -->

### **Python3**

```python

class Solution:
def findMissingAndRepeatedValues(self, grid: List[List[int]]) -> List[int]:
n = len(grid)
cnt = [0] * (n * n + 1)
for row in grid:
for v in row:
cnt[v] += 1
ans = [0] * 2
for i in range(1, n * n + 1):
if cnt[i] == 2:
ans[0] = i
if cnt[i] == 0:
ans[1] = i
return ans
```

### **Java**

```java

class Solution {
public int[] findMissingAndRepeatedValues(int[][] grid) {
int n = grid.length;
int[] cnt = new int[n * n + 1];
int[] ans = new int[2];
for (int[] row : grid) {
for (int x : row) {
if (++cnt[x] == 2) {
ans[0] = x;
}
}
}
for (int x = 1;; ++x) {
if (cnt[x] == 0) {
ans[1] = x;
return ans;
}
}
}
}
```

### **C++**

```cpp

class Solution {
public:
vector<int> findMissingAndRepeatedValues(vector<vector<int>>& grid) {
int n = grid.size();
vector<int> cnt(n * n + 1);
vector<int> ans(2);
for (auto& row : grid) {
for (int x : row) {
if (++cnt[x] == 2) {
ans[0] = x;
}
}
}
for (int x = 1;; ++x) {
if (cnt[x] == 0) {
ans[1] = x;
return ans;
}
}
}
};
```

### **Go**

```go
func findMissingAndRepeatedValues(grid [][]int) []int {
n := len(grid)
ans := make([]int, 2)
cnt := make([]int, n*n+1)
for _, row := range grid {
for _, x := range row {
cnt[x]++
if cnt[x] == 2 {
ans[0] = x
}
}
}
for x := 1; ; x++ {
if cnt[x] == 0 {
ans[1] = x
return ans
}
}
}
```

### **TypeScript**

```ts
function findMissingAndRepeatedValues(grid: number[][]): number[] {
const n = grid.length;
const cnt: number[] = Array(n * n + 1).fill(0);
const ans: number[] = Array(2).fill(0);
for (const row of grid) {
for (const x of row) {
if (++cnt[x] === 2) {
ans[0] = x;
}
}
}
for (let x = 1; ; ++x) {
if (cnt[x] === 0) {
ans[1] = x;
return ans;
}
}
}
```

### **...**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public:
vector<int> findMissingAndRepeatedValues(vector<vector<int>>& grid) {
int n = grid.size();
vector<int> cnt(n * n + 1);
vector<int> ans(2);
for (auto& row : grid) {
for (int x : row) {
if (++cnt[x] == 2) {
ans[0] = x;
}
}
}
for (int x = 1;; ++x) {
if (cnt[x] == 0) {
ans[1] = x;
return ans;
}
}
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
func findMissingAndRepeatedValues(grid [][]int) []int {
n := len(grid)
ans := make([]int, 2)
cnt := make([]int, n*n+1)
for _, row := range grid {
for _, x := range row {
cnt[x]++
if cnt[x] == 2 {
ans[0] = x
}
}
}
for x := 1; ; x++ {
if cnt[x] == 0 {
ans[1] = x
return ans
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
public int[] findMissingAndRepeatedValues(int[][] grid) {
int n = grid.length;
int[] cnt = new int[n * n + 1];
int[] ans = new int[2];
for (int[] row : grid) {
for (int x : row) {
if (++cnt[x] == 2) {
ans[0] = x;
}
}
}
for (int x = 1;; ++x) {
if (cnt[x] == 0) {
ans[1] = x;
return ans;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution:
def findMissingAndRepeatedValues(self, grid: List[List[int]]) -> List[int]:
n = len(grid)
cnt = [0] * (n * n + 1)
for row in grid:
for v in row:
cnt[v] += 1
ans = [0] * 2
for i in range(1, n * n + 1):
if cnt[i] == 2:
ans[0] = i
if cnt[i] == 0:
ans[1] = i
return ans
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function findMissingAndRepeatedValues(grid: number[][]): number[] {
const n = grid.length;
const cnt: number[] = Array(n * n + 1).fill(0);
const ans: number[] = Array(2).fill(0);
for (const row of grid) {
for (const x of row) {
if (++cnt[x] === 2) {
ans[0] = x;
}
}
}
for (let x = 1; ; ++x) {
if (cnt[x] === 0) {
ans[1] = x;
return ans;
}
}
}
Loading