diff --git a/leetcode/2401-2500/2442.Count-Number-of-Distinct-Integers-After-Reverse-Operations/README.md b/leetcode/2401-2500/2442.Count-Number-of-Distinct-Integers-After-Reverse-Operations/README.md index 6b4e36963..a2ed710e3 100755 --- a/leetcode/2401-2500/2442.Count-Number-of-Distinct-Integers-After-Reverse-Operations/README.md +++ b/leetcode/2401-2500/2442.Count-Number-of-Distinct-Integers-After-Reverse-Operations/README.md @@ -1,28 +1,30 @@ # [2442.Count Number of Distinct Integers After Reverse Operations][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 array `nums` consisting of **positive** integers. + +You have to take each integer in the array, **reverse its digits**, and add it to the end of the array. You should apply this operation to the original integers in `nums`. + +Return the number of **distinct** integers in the final array. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: nums = [1,13,10,12,31] +Output: 6 +Explanation: After including the reverse of each number, the resulting array is [1,13,10,12,31,1,31,1,21,13]. +The reversed integers that were added to the end of the array are underlined. Note that for the integer 10, after reversing it, it becomes 01 which is just 1. +The number of distinct integers in this array is 6 (The numbers 1, 10, 12, 13, 21, and 31). ``` -## 题意 -> ... +**Example 2:** -## 题解 - -### 思路1 -> ... -Count Number of Distinct Integers After Reverse Operations -```go ``` - +Input: nums = [2,2,2] +Output: 1 +Explanation: After including the reverse of each number, the resulting array is [2,2,2,2,2,2]. +The number of distinct integers in this array is 1 (The number 2). +``` ## 结语 diff --git a/leetcode/2401-2500/2442.Count-Number-of-Distinct-Integers-After-Reverse-Operations/Solution.go b/leetcode/2401-2500/2442.Count-Number-of-Distinct-Integers-After-Reverse-Operations/Solution.go index d115ccf5e..2fd2159fe 100644 --- a/leetcode/2401-2500/2442.Count-Number-of-Distinct-Integers-After-Reverse-Operations/Solution.go +++ b/leetcode/2401-2500/2442.Count-Number-of-Distinct-Integers-After-Reverse-Operations/Solution.go @@ -1,5 +1,19 @@ package Solution -func Solution(x bool) bool { +func rev(n int) int { + x := 0 + for n > 0 { + digit := n % 10 + n /= 10 + x = x*10 + digit + } return x } +func Solution(nums []int) int { + set := make(map[int]struct{}) + for _, n := range nums { + set[n] = struct{}{} + set[rev(n)] = struct{}{} + } + return len(set) +} diff --git a/leetcode/2401-2500/2442.Count-Number-of-Distinct-Integers-After-Reverse-Operations/Solution_test.go b/leetcode/2401-2500/2442.Count-Number-of-Distinct-Integers-After-Reverse-Operations/Solution_test.go index 14ff50eb4..1462fcdc8 100644 --- a/leetcode/2401-2500/2442.Count-Number-of-Distinct-Integers-After-Reverse-Operations/Solution_test.go +++ b/leetcode/2401-2500/2442.Count-Number-of-Distinct-Integers-After-Reverse-Operations/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{1, 13, 10, 12, 31}, 6}, + {"TestCase2", []int{2, 2, 2}, 1}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }