Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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() {
}
Loading