Skip to content

Commit 469cd25

Browse files
authored
Merge pull request #1233 from 0xff-dev/2566
Add solution and test-cases for problem 2566
2 parents 976d01f + 210bb1e commit 469cd25

File tree

3 files changed

+83
-9
lines changed

3 files changed

+83
-9
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# [2566.Maximum Difference by Remapping a Digit][title]
2+
3+
## Description
4+
5+
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.
6+
7+
Return the difference between the maximum and minimum values Bob can make by remapping **exactly one** digit in `num`.
8+
9+
**Notes**:
10+
11+
- When Bob remaps a digit d1 to another digit d2, Bob replaces all occurrences of `d1` in `num` with `d2`.
12+
- Bob can remap a digit to itself, in which case `num` does not change.
13+
- Bob can remap different digits for obtaining minimum and maximum values respectively.
14+
- The resulting number after remapping can contain leading zeroes.
15+
16+
**Example 1:**
17+
18+
```
19+
Input: num = 11891
20+
Output: 99009
21+
Explanation:
22+
To achieve the maximum value, Bob can remap the digit 1 to the digit 9 to yield 99899.
23+
To achieve the minimum value, Bob can remap the digit 1 to the digit 0, yielding 890.
24+
The difference between these two numbers is 99009.
25+
```
26+
27+
**Example 2:**
28+
29+
```
30+
Input: num = 90
31+
Output: 99
32+
Explanation:
33+
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).
34+
Thus, we return 99.
35+
```
36+
37+
## 结语
38+
39+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
40+
41+
[title]: https://leetcode.com/problems/maximum-difference-by-remapping-a-digit
42+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,38 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(num int) int {
4+
digits := make([]int, 0)
5+
for num > 0 {
6+
digits = append(digits, num%10)
7+
num /= 10
8+
}
9+
cp1 := make([]int, len(digits))
10+
copy(cp1, digits)
11+
i := len(digits) - 1
12+
remap := -1
13+
for ; i >= 0; i-- {
14+
if cp1[i] != 9 {
15+
remap = cp1[i]
16+
break
17+
}
18+
}
19+
for ; i >= 0; i-- {
20+
if cp1[i] == remap {
21+
cp1[i] = 9
22+
}
23+
}
24+
25+
i = len(digits) - 1
26+
remap = digits[i]
27+
for ; i >= 0; i-- {
28+
if digits[i] == remap {
29+
digits[i] = 0
30+
}
31+
}
32+
a, b := 0, 0
33+
for i := len(digits) - 1; i >= 0; i-- {
34+
a = a*10 + cp1[i]
35+
b = b*10 + digits[i]
36+
}
37+
return a - b
538
}

leetcode/2501-2600/2566.Maximum-Difference-by-Remapping-a-Digit/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", 11891, 99009},
17+
{"TestCase2", 90, 99},
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)