From 620bd1af1aa05c0e85bde415f1a2c3da166f6cee Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Fri, 31 Oct 2025 09:48:29 +0800 Subject: [PATCH] Add solution and test-cases for problem 3289 --- .../README.md | 49 +++++++++++++++++++ .../Solution.go | 16 ++++++ .../Solution_test.go | 41 ++++++++++++++++ 3 files changed, 106 insertions(+) create mode 100644 leetcode/3201-3300/3289.The-Two-Sneaky-Numbers-of-Digitville/README.md create mode 100644 leetcode/3201-3300/3289.The-Two-Sneaky-Numbers-of-Digitville/Solution.go create mode 100644 leetcode/3201-3300/3289.The-Two-Sneaky-Numbers-of-Digitville/Solution_test.go diff --git a/leetcode/3201-3300/3289.The-Two-Sneaky-Numbers-of-Digitville/README.md b/leetcode/3201-3300/3289.The-Two-Sneaky-Numbers-of-Digitville/README.md new file mode 100644 index 00000000..16930560 --- /dev/null +++ b/leetcode/3201-3300/3289.The-Two-Sneaky-Numbers-of-Digitville/README.md @@ -0,0 +1,49 @@ +# [3289.The Two Sneaky Numbers of Digitville][title] + +## Description +In the town of Digitville, there was a list of numbers called `nums` containing integers from `0` to `n - 1`. Each number was supposed to appear **exactly once** in the list, however, **two** mischievous numbers sneaked in an additional time, making the list longer than usual. + +As the town detective, your task is to find these **two** sneaky numbers. Return an array of size two containing the two numbers (in any order), so peace can return to Digitville. + +**Example 1:** + +``` +Input: nums = [0,1,1,0] + +Output: [0,1] + +Explanation: + +The numbers 0 and 1 each appear twice in the array. +``` + +**Example 2:** + +``` +Input: nums = [0,3,2,1,3,2] + +Output: [2,3] + +Explanation: + +The numbers 2 and 3 each appear twice in the array. +``` + +**Example 3:** + +``` +Input: nums = [7,1,5,4,3,4,6,0,9,5,8,2] + +Output: [4,5] + +Explanation: + +The numbers 4 and 5 each appear twice in the array. +``` + +## 结语 + +如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] + +[title]: https://leetcode.com/problems/the-two-sneaky-numbers-of-digitville +[me]: https://github.com/kylesliu/awesome-golang-algorithm diff --git a/leetcode/3201-3300/3289.The-Two-Sneaky-Numbers-of-Digitville/Solution.go b/leetcode/3201-3300/3289.The-Two-Sneaky-Numbers-of-Digitville/Solution.go new file mode 100644 index 00000000..587844c4 --- /dev/null +++ b/leetcode/3201-3300/3289.The-Two-Sneaky-Numbers-of-Digitville/Solution.go @@ -0,0 +1,16 @@ +package Solution + +func Solution(nums []int) []int { + ret := make([]int, 2) + index := 0 + exists := make([]bool, len(nums)-2) + for _, n := range nums { + if exists[n] { + ret[index] = n + index++ + continue + } + exists[n] = true + } + return ret +} diff --git a/leetcode/3201-3300/3289.The-Two-Sneaky-Numbers-of-Digitville/Solution_test.go b/leetcode/3201-3300/3289.The-Two-Sneaky-Numbers-of-Digitville/Solution_test.go new file mode 100644 index 00000000..8cb103a6 --- /dev/null +++ b/leetcode/3201-3300/3289.The-Two-Sneaky-Numbers-of-Digitville/Solution_test.go @@ -0,0 +1,41 @@ +package Solution + +import ( + "reflect" + "sort" + "strconv" + "testing" +) + +func TestSolution(t *testing.T) { + // 测试用例 + cases := []struct { + name string + inputs []int + expect []int + }{ + {"TestCase1", []int{0, 1, 1, 0}, []int{0, 1}}, + {"TestCase2", []int{0, 3, 2, 1, 3, 2}, []int{2, 3}}, + {"TestCase3", []int{7, 1, 5, 4, 3, 4, 6, 0, 9, 5, 8, 2}, []int{4, 5}}, + } + + // 开始测试 + for i, c := range cases { + t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { + got := Solution(c.inputs) + sort.Ints(got) + if !reflect.DeepEqual(got, c.expect) { + t.Fatalf("expected: %v, but got: %v, with inputs: %v", + c.expect, got, c.inputs) + } + }) + } +} + +// 压力测试 +func BenchmarkSolution(b *testing.B) { +} + +// 使用案列 +func ExampleSolution() { +}