Skip to content

Add solution and test-cases for problem 2566 #1233

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# [2566.Maximum Difference by Remapping a Digit][title]

## Description

You are given an integer `num`. You know that Bob will sneakily **remap** one of the `10` possible digits (`0` to `9`) to another digit.

Return the difference between the maximum and minimum values Bob can make by remapping **exactly one** digit in `num`.

**Notes**:

- When Bob remaps a digit d1 to another digit d2, Bob replaces all occurrences of `d1` in `num` with `d2`.
- Bob can remap a digit to itself, in which case `num` does not change.
- Bob can remap different digits for obtaining minimum and maximum values respectively.
- The resulting number after remapping can contain leading zeroes.

**Example 1:**

```
Input: num = 11891
Output: 99009
Explanation:
To achieve the maximum value, Bob can remap the digit 1 to the digit 9 to yield 99899.
To achieve the minimum value, Bob can remap the digit 1 to the digit 0, yielding 890.
The difference between these two numbers is 99009.
```

**Example 2:**

```
Input: num = 90
Output: 99
Explanation:
The maximum value that can be returned by the function is 99 (if 0 is replaced by 9) and the minimum value that can be returned by the function is 0 (if 9 is replaced by 0).
Thus, we return 99.
```

## 结语

如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]

[title]: https://leetcode.com/problems/maximum-difference-by-remapping-a-digit
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Original file line number Diff line number Diff line change
@@ -1,5 +1,38 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(num int) int {
digits := make([]int, 0)
for num > 0 {
digits = append(digits, num%10)
num /= 10
}
cp1 := make([]int, len(digits))
copy(cp1, digits)
i := len(digits) - 1
remap := -1
for ; i >= 0; i-- {
if cp1[i] != 9 {
remap = cp1[i]
break
}
}
for ; i >= 0; i-- {
if cp1[i] == remap {
cp1[i] = 9
}
}

i = len(digits) - 1
remap = digits[i]
for ; i >= 0; i-- {
if digits[i] == remap {
digits[i] = 0
}
}
a, b := 0, 0
for i := len(digits) - 1; i >= 0; i-- {
a = a*10 + cp1[i]
b = b*10 + digits[i]
}
return a - b
}
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", 11891, 99009},
{"TestCase2", 90, 99},
}

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

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

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