diff --git a/leetcode/2101-2200/2150.Find-All-Lonely-Numbers-in-the-Array/README.md b/leetcode/2101-2200/2150.Find-All-Lonely-Numbers-in-the-Array/README.md index 9c97bca05..a31500785 100755 --- a/leetcode/2101-2200/2150.Find-All-Lonely-Numbers-in-the-Array/README.md +++ b/leetcode/2101-2200/2150.Find-All-Lonely-Numbers-in-the-Array/README.md @@ -1,28 +1,35 @@ # [2150.Find All Lonely Numbers in the Array][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +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. + +Return **all** lonely numbers in `nums`. You may return the answer in **any order**. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: nums = [10,6,5,8] +Output: [10,8] +Explanation: +- 10 is a lonely number since it appears exactly once and 9 and 11 does not appear in nums. +- 8 is a lonely number since it appears exactly once and 7 and 9 does not appear in nums. +- 5 is not a lonely number since 6 appears in nums and vice versa. +Hence, the lonely numbers in nums are [10, 8]. +Note that [8, 10] may also be returned. ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Find All Lonely Numbers in the Array -```go ``` - +Input: nums = [1,3,5,3] +Output: [1,5] +Explanation: +- 1 is a lonely number since it appears exactly once and 0 and 2 does not appear in nums. +- 5 is a lonely number since it appears exactly once and 4 and 6 does not appear in nums. +- 3 is not a lonely number since it appears twice. +Hence, the lonely numbers in nums are [1, 5]. +Note that [5, 1] may also be returned. +``` ## 结语 diff --git a/leetcode/2101-2200/2150.Find-All-Lonely-Numbers-in-the-Array/Solution.go b/leetcode/2101-2200/2150.Find-All-Lonely-Numbers-in-the-Array/Solution.go index d115ccf5e..a55679ee0 100644 --- a/leetcode/2101-2200/2150.Find-All-Lonely-Numbers-in-the-Array/Solution.go +++ b/leetcode/2101-2200/2150.Find-All-Lonely-Numbers-in-the-Array/Solution.go @@ -1,5 +1,20 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(nums []int) []int { + cnt := make(map[int]int) + for _, n := range nums { + cnt[n]++ + } + ans := make([]int, 0) + for _, n := range nums { + if cnt[n] != 1 { + continue + } + _, ok1 := cnt[n-1] + _, ok2 := cnt[n+1] + if !ok1 && !ok2 { + ans = append(ans, n) + } + } + return ans } diff --git a/leetcode/2101-2200/2150.Find-All-Lonely-Numbers-in-the-Array/Solution_test.go b/leetcode/2101-2200/2150.Find-All-Lonely-Numbers-in-the-Array/Solution_test.go index 14ff50eb4..8412a8a15 100644 --- a/leetcode/2101-2200/2150.Find-All-Lonely-Numbers-in-the-Array/Solution_test.go +++ b/leetcode/2101-2200/2150.Find-All-Lonely-Numbers-in-the-Array/Solution_test.go @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs []int + expect []int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{10, 6, 5, 8}, []int{10, 8}}, + {"TestCase2", []int{1, 3, 5, 3}, []int{1, 5}}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }