Skip to content

Commit 78d78c1

Browse files
committed
Add solution and test-cases for problem 2150
1 parent 31756cd commit 78d78c1

File tree

3 files changed

+44
-23
lines changed

3 files changed

+44
-23
lines changed

leetcode/2101-2200/2150.Find-All-Lonely-Numbers-in-the-Array/README.md

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
11
# [2150.Find All Lonely Numbers in the Array][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given an integer array `nums`. A number `x` is **lonely** when it appears only **once**, and no **adjacent** numbers (i.e. `x + 1` and `x - 1`) appear in the array.
5+
6+
Return **all** lonely numbers in `nums`. You may return the answer in **any order**.
77

88
**Example 1:**
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
11+
Input: nums = [10,6,5,8]
12+
Output: [10,8]
13+
Explanation:
14+
- 10 is a lonely number since it appears exactly once and 9 and 11 does not appear in nums.
15+
- 8 is a lonely number since it appears exactly once and 7 and 9 does not appear in nums.
16+
- 5 is not a lonely number since 6 appears in nums and vice versa.
17+
Hence, the lonely numbers in nums are [10, 8].
18+
Note that [8, 10] may also be returned.
1319
```
1420

15-
## 题意
16-
> ...
17-
18-
## 题解
21+
**Example 2:**
1922

20-
### 思路1
21-
> ...
22-
Find All Lonely Numbers in the Array
23-
```go
2423
```
25-
24+
Input: nums = [1,3,5,3]
25+
Output: [1,5]
26+
Explanation:
27+
- 1 is a lonely number since it appears exactly once and 0 and 2 does not appear in nums.
28+
- 5 is a lonely number since it appears exactly once and 4 and 6 does not appear in nums.
29+
- 3 is not a lonely number since it appears twice.
30+
Hence, the lonely numbers in nums are [1, 5].
31+
Note that [5, 1] may also be returned.
32+
```
2633

2734
## 结语
2835

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int) []int {
4+
cnt := make(map[int]int)
5+
for _, n := range nums {
6+
cnt[n]++
7+
}
8+
ans := make([]int, 0)
9+
for _, n := range nums {
10+
if cnt[n] != 1 {
11+
continue
12+
}
13+
_, ok1 := cnt[n-1]
14+
_, ok2 := cnt[n+1]
15+
if !ok1 && !ok2 {
16+
ans = append(ans, n)
17+
}
18+
}
19+
return ans
520
}

leetcode/2101-2200/2150.Find-All-Lonely-Numbers-in-the-Array/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
expect []int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{10, 6, 5, 8}, []int{10, 8}},
17+
{"TestCase2", []int{1, 3, 5, 3}, []int{1, 5}},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)