Skip to content

Add solution and test-cases for problem 2965 #1141

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
Mar 7, 2025
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
25 changes: 11 additions & 14 deletions leetcode/2901-3000/2965.Find-Missing-and-Repeated-Values/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
# [2965.Find Missing and Repeated Values][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

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

Return a **0-indexed** integer array `ans` of size `2` where `ans[0]` equals to `a` and `ans[1]` equals to `b`.

**Example 1:**

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

## 题意
> ...

## 题解
**Example 2:**

### 思路1
> ...
Find Missing and Repeated Values
```go
```

Input: grid = [[9,1,7],[8,9,2],[3,4,6]]
Output: [9,5]
Explanation: Number 9 is repeated and number 5 is missing so the answer is [9,5].
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(grid [][]int) []int {
m := len(grid)
base, sum := m*m, 0
n := make([]int, base)
target := base * (base + 1) / 2
repeate := 1
for i := 0; i < m; i++ {
for j := 0; j < m; j++ {
n[grid[i][j]-1]++
if n[grid[i][j]-1] == 2 {
repeate = grid[i][j]
}
sum += grid[i][j]
}
}
diff := sum - target
ans := []int{repeate, repeate - diff}
return ans
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs [][]int
expect []int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", [][]int{{1, 3}, {2, 2}}, []int{2, 4}},
{"TestCase2", [][]int{{9, 1, 7}, {8, 9, 2}, {3, 4, 6}}, []int{9, 5}},
}

// 开始测试
Expand All @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading