Skip to content

Commit

Permalink
Merge pull request #665 from 0xff-dev/2442
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 2442
  • Loading branch information
6boris authored Oct 28, 2023
2 parents 81ddc26 + c1cc258 commit df172a9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -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).
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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},
}

// 开始测试
Expand All @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}

0 comments on commit df172a9

Please sign in to comment.