Skip to content

Commit cbb1936

Browse files
authored
Merge pull request #1141 from 0xff-dev/2965
Add solution and test-cases for problem 2965
2 parents b1420c9 + 5f426af commit cbb1936

File tree

3 files changed

+35
-23
lines changed

3 files changed

+35
-23
lines changed

leetcode/2901-3000/2965.Find-Missing-and-Repeated-Values/README.md

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
11
# [2965.Find Missing and Repeated Values][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given a **0-indexed** 2D integer matrix `grid` of size `n * n` with values in the range `[1, n^2]`. Each integer appears **exactly once** except `a` which appears **twice** and `b` which is **missing**. The task is to find the repeating and missing numbers `a` and `b`.
5+
6+
Return a **0-indexed** integer array `ans` of size `2` where `ans[0]` equals to `a` and `ans[1]` equals to `b`.
77

88
**Example 1:**
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
11+
Input: grid = [[1,3],[2,2]]
12+
Output: [2,4]
13+
Explanation: Number 2 is repeated and number 4 is missing so the answer is [2,4].
1314
```
1415

15-
## 题意
16-
> ...
17-
18-
## 题解
16+
**Example 2:**
1917

20-
### 思路1
21-
> ...
22-
Find Missing and Repeated Values
23-
```go
2418
```
25-
19+
Input: grid = [[9,1,7],[8,9,2],[3,4,6]]
20+
Output: [9,5]
21+
Explanation: Number 9 is repeated and number 5 is missing so the answer is [9,5].
22+
```
2623

2724
## 结语
2825

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(grid [][]int) []int {
4+
m := len(grid)
5+
base, sum := m*m, 0
6+
n := make([]int, base)
7+
target := base * (base + 1) / 2
8+
repeate := 1
9+
for i := 0; i < m; i++ {
10+
for j := 0; j < m; j++ {
11+
n[grid[i][j]-1]++
12+
if n[grid[i][j]-1] == 2 {
13+
repeate = grid[i][j]
14+
}
15+
sum += grid[i][j]
16+
}
17+
}
18+
diff := sum - target
19+
ans := []int{repeate, repeate - diff}
20+
return ans
521
}

leetcode/2901-3000/2965.Find-Missing-and-Repeated-Values/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs [][]int
14+
expect []int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", [][]int{{1, 3}, {2, 2}}, []int{2, 4}},
17+
{"TestCase2", [][]int{{9, 1, 7}, {8, 9, 2}, {3, 4, 6}}, []int{9, 5}},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)