8000 feat: solved 448. Find All Numbers Disappeared in an Array Β· realtemirov/leetcode@52925a7 Β· GitHub
[go: up one dir, main page]

Skip to content

Commit 52925a7

Browse files
committed
feat: solved 448. Find All Numbers Disappeared in an Array
1 parent 4b92e67 commit 52925a7

7 files changed

+87
-65
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# 448. Find All Numbers Disappeared in an Array
2+
3+
🟩 Easy
4+
5+
## Solution
6+
7+
My Solution
8+
9+
```go
10+
func findDisappearedNumbers(nums []int) []int {
11+
arr := make([]bool, len(nums))
12+
13+
for _, num :=range nums {
14+
arr[num-1] = true
15+
}
16+
17+
idx := 0
18+
for i, b := range arr {
19+
if !b {
20+
nums[idx] = i+1
21+
idx++
22+
}
23+
}
24+
25+
return nums[:idx]
26+
}
27+
```
28+
29+
![result](0448.png)
30+
31+
Leetcode: [link](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/description/)

β€ŽProblems/0448/448.png

37.8 KB
Loading

β€ŽProblems/0448/solution.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package problem0448
2+
3+
func FindDisappearedNumbers(nums []int) []int {
4+
arr := make([]bool, len(nums))
5+
6+
for _, num :=range nums {
7+
arr[num-1] = true
8+
}
9+
10+
idx := 0
11+
for i, b := range arr {
12+
if !b {
13+
nums[idx] = i+1
14+
idx++
15+
}
16+
}
17+
18+
return nums[:idx]
19+
}

β€ŽProblems/0448/solution_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package problem0448_test
2+
3+
import (
4+
"testing"
5+
6+
problem0448 "github.com/realtemirov/leetcode/Problems/0448"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestSolution(t *testing.T) {
11+
testCases := []struct {
12+
name string
13+
cases []int
14+
expected []int
15+
}{
16+
{
17+
name: "Test 1",
18+
cases: []int{4, 3, 2, 7, 8, 2, 3, 1},
19+
expected: []int{5, 6},
20+
},
21+
{
22+
name: "Test 2",
23+
cases: []int{1, 1},
24+
expected: []int{2},
25+
},
26+
}
27+
28+
for _, tc := range testCases {
29+
t.Run(tc.name, func(t *testing.T) {
30+
result := problem0448.FindDisappearedNumbers(tc.cases)
31+
require.Equal(t, tc.expected, result, "expected: %v, result: %v", tc.expected, result)
32+
})
33+
}
34+
}

β€ŽProblems/448. Find All Numbers Disappeared in an Array.md

Lines changed: 0 additions & 64 deletions
This file was deleted.

β€ŽProblems/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
* 🟩 Easy - [412. Fizz Buzz](<./0412/412. Fizz Buzz.md>)
4848
* 🟩 Easy - [414. Third Maximum Number](<./0414/414. Third Maximum Number.md>)
4949
* 🟧 Medium - [445. Add Two Numbers II](<./0445/445. Add Two Numbers II.md>)
50+
* 🟩 Easy - [448. Find All Numbers Disappeared in an Array](<./0448/448. Find All Numbers Disappeared in an Array.md>)
5051
* 🟩 Easy - [459. Repeated Substring Pattern](<./0459/459. Repeated Substring Pattern.md>)
5152
* 🟩 Easy - [485. Max Consecutive Ones](<./0485/485. Max Consecutive Ones.md>)
5253
* 🟩 Easy - [509. Fibonacci Number](<./0509/509. Fibonacci Number.md>)

β€ŽSUMMARY.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
* [412. Fizz Buzz](<./Problems/0412/412. Fizz Buzz.md>)
103103
* [414. Third Maximum Number](<./Problems/0414/414. Third Maximum Number.md>)
104104
* [445. Add Two Numbers II](<./Problems/0445/445. Add Two Numbers II.md>)
105+
* [448. Find All Numbers Disappeared in an Array](<./Problems/0448/448. Find All Numbers Disappeared in an Array.md>)
105106
* [459. Repeated Substring Pattern](<./Problems/0459/459. Repeated Substring Pattern.md>)
106107
* [485. Max Consecutive Ones](<./Problems/0485/485. Max Consecutive Ones.md>)
107108
* [509. Fibonacci Number](<./Problems/0509/509. Fibonacci Number.md>)
@@ -192,7 +193,7 @@
192193
193194
194195
* [443. String Compression](<Problems/443. String Compression.md>)
195-
* [448. Find All Numbers Disappeared in an Array](<Problems/448. Find All Numbers Disappeared in an Array.md>)
196+
196197
* [451. Sort Characters By Frequency](<Problems/451. Sort Characters By Frequency.md>)
197198
* [455. Assign Cookies](<Problems/455. Assign Cookies.md>)
198199

0 commit comments

Comments
Β (0)
0