Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0cad587

Browse files
author
Sandy
authored
Merge pull request #194 from openset/develop
Add: Find All Numbers Disappeared in an Array
2 parents 402ac63 + 0344838 commit 0cad587

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,16 @@
11
package find_all_numbers_disappeared_in_an_array
2+
3+
func findDisappearedNumbers(nums []int) []int {
4+
max, count := len(nums), 0
5+
s := make([]bool, max)
6+
for _, v := range nums {
7+
s[v-1] = true
8+
}
9+
for i := 1; i <= max; i++ {
10+
if !s[i-1] {
11+
nums[count] = i
12+
count++
13+
}
14+
}
15+
return nums[:count]
16+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,34 @@
11
package find_all_numbers_disappeared_in_an_array
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
type caseType struct {
9+
input []int
10+
expected []int
11+
}
12+
13+
func TestFindDisappearedNumbers(t *testing.T) {
14+
tests := [...]caseType{
15+
{
16+
input: []int{4, 3, 2, 7, 8, 2, 3, 1},
17+
expected: []int{5, 6},
18+
},
19+
{
20+
input: []int{5, 4, 2, 3, 1},
21+
expected: []int{},
22+
},
23+
{
24+
input: []int{1, 1},
25+
expected: []int{2},
26+
},
27+
}
28+
for _, tc := range tests {
29+
output := findDisappearedNumbers(tc.input)
30+
if !reflect.DeepEqual(output, tc.expected) {
31+
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
32+
}
33+
}
34+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /