Skip to content

Commit 620bd1a

Browse files
committed
Add solution and test-cases for problem 3289
1 parent f03226b commit 620bd1a

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# [3289.The Two Sneaky Numbers of Digitville][title]
2+
3+
## Description
4+
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.
5+
6+
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.
7+
8+
**Example 1:**
9+
10+
```
11+
Input: nums = [0,1,1,0]
12+
13+
Output: [0,1]
14+
15+
Explanation:
16+
17+
The numbers 0 and 1 each appear twice in the array.
18+
```
19+
20+
**Example 2:**
21+
22+
```
23+
Input: nums = [0,3,2,1,3,2]
24+
25+
Output: [2,3]
26+
27+
Explanation:
28+
29+
The numbers 2 and 3 each appear twice in the array.
30+
```
31+
32+
**Example 3:**
33+
34+
```
35+
Input: nums = [7,1,5,4,3,4,6,0,9,5,8,2]
36+
37+
Output: [4,5]
38+
39+
Explanation:
40+
41+
The numbers 4 and 5 each appear twice in the array.
42+
```
43+
44+
## 结语
45+
46+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
47+
48+
[title]: https://leetcode.com/problems/the-two-sneaky-numbers-of-digitville
49+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package Solution
2+
3+
func Solution(nums []int) []int {
4+
ret := make([]int, 2)
5+
index := 0
6+
exists := make([]bool, len(nums)-2)
7+
for _, n := range nums {
8+
if exists[n] {
9+
ret[index] = n
10+
index++
11+
continue
12+
}
13+
exists[n] = true
14+
}
15+
return ret
16+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package Solution
2+
3+
import (
4+
"reflect"
5+
"sort"
6+
"strconv"
7+
"testing"
8+
)
9+
10+
func TestSolution(t *testing.T) {
11+
// 测试用例
12+
cases := []struct {
13+
name string
14+
inputs []int
15+
expect []int
16+
}{
17+
{"TestCase1", []int{0, 1, 1, 0}, []int{0, 1}},
18+
{"TestCase2", []int{0, 3, 2, 1, 3, 2}, []int{2, 3}},
19+
{"TestCase3", []int{7, 1, 5, 4, 3, 4, 6, 0, 9, 5, 8, 2}, []int{4, 5}},
20+
}
21+
22+
// 开始测试
23+
for i, c := range cases {
24+
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
25+
got := Solution(c.inputs)
26+
sort.Ints(got)
27+
if !reflect.DeepEqual(got, c.expect) {
28+
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
29+
c.expect, got, c.inputs)
30+
}
31+
})
32+
}
33+
}
34+
35+
// 压力测试
36+
func BenchmarkSolution(b *testing.B) {
37+
}
38+
39+
// 使用案列
40+
func ExampleSolution() {
41+
}

0 commit comments

Comments
 (0)