diff --git a/leetcode/2901-3000/2965.Find-Missing-and-Repeated-Values/README.md b/leetcode/2901-3000/2965.Find-Missing-and-Repeated-Values/README.md index ef1e2e001..0b1e75170 100755 --- a/leetcode/2901-3000/2965.Find-Missing-and-Repeated-Values/README.md +++ b/leetcode/2901-3000/2965.Find-Missing-and-Repeated-Values/README.md @@ -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]. +``` ## 结语 diff --git a/leetcode/2901-3000/2965.Find-Missing-and-Repeated-Values/Solution.go b/leetcode/2901-3000/2965.Find-Missing-and-Repeated-Values/Solution.go index d115ccf5e..29d0da8a5 100644 --- a/leetcode/2901-3000/2965.Find-Missing-and-Repeated-Values/Solution.go +++ b/leetcode/2901-3000/2965.Find-Missing-and-Repeated-Values/Solution.go @@ -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 } diff --git a/leetcode/2901-3000/2965.Find-Missing-and-Repeated-Values/Solution_test.go b/leetcode/2901-3000/2965.Find-Missing-and-Repeated-Values/Solution_test.go index 14ff50eb4..d32d63cb4 100644 --- a/leetcode/2901-3000/2965.Find-Missing-and-Repeated-Values/Solution_test.go +++ b/leetcode/2901-3000/2965.Find-Missing-and-Repeated-Values/Solution_test.go @@ -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}}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }