Skip to content

Commit 976d01f

Browse files
authored
Merge pull request #1234 from 0xff-dev/1432
Add solution and test-cases for problem 1432
2 parents dea1851 + 81167d8 commit 976d01f

File tree

3 files changed

+98
-9
lines changed

3 files changed

+98
-9
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# [1432.Max Difference You Can Get From Changing an Integer][title]
2+
3+
## Description
4+
5+
You are given an integer `num`. You will apply the following steps to num **two** separate times:
6+
7+
- Pick a digit `x` `(0 <= x <= 9)`.
8+
- Pick another digit `y` `(0 <= y <= 9)`. Note `y` can be equal to `x`.
9+
- Replace all the occurrences of `x` in the decimal representation of `num` by `y`.
10+
11+
Let `a` and `b` be the two results from applying the operation to `num` independently.
12+
13+
Return the max difference between `a` and `b`.
14+
15+
Note that neither `a` nor `b` may have any leading zeros, and **must not** be 0.
16+
17+
**Example 1:**
18+
19+
```
20+
Input: num = 555
21+
Output: 888
22+
Explanation: The first time pick x = 5 and y = 9 and store the new integer in a.
23+
The second time pick x = 5 and y = 1 and store the new integer in b.
24+
We have now a = 999 and b = 111 and max difference = 888
25+
```
26+
27+
**Example 2:**
28+
29+
```
30+
Input: num = 9
31+
Output: 8
32+
Explanation: The first time pick x = 9 and y = 9 and store the new integer in a.
33+
The second time pick x = 9 and y = 1 and store the new integer in b.
34+
We have now a = 9 and b = 1 and max difference = 8
35+
```
36+
37+
## 结语
38+
39+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
40+
41+
[title]: https://leetcode.com/problems/max-difference-you-can-get-from-changing-an-integer/
42+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,53 @@
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+
cp := make([]int, len(digits))
10+
copy(cp, digits)
11+
12+
i := len(digits) - 1
13+
remap := -1
14+
for ; i >= 0; i-- {
15+
if cp[i] != 9 {
16+
remap = cp[i]
17+
break
18+
}
19+
}
20+
for ; i >= 0; i-- {
21+
if cp[i] == remap {
22+
cp[i] = 9
23+
}
24+
}
25+
26+
i = len(digits) - 1
27+
target := 1
28+
if digits[i] == 1 {
29+
target = 0
30+
}
31+
32+
remap = -1
33+
for ; i >= 0; i-- {
34+
if digits[i] == 0 {
35+
continue
36+
}
37+
if digits[i] != 1 {
38+
remap = digits[i]
39+
break
40+
}
41+
}
42+
for ; i >= 0; i-- {
43+
if digits[i] == remap {
44+
digits[i] = target
45+
}
46+
}
47+
a, b := 0, 0
48+
for i := len(digits) - 1; i >= 0; i-- {
49+
a = a*10 + cp[i]
50+
b = b*10 + digits[i]
51+
}
52+
return a - b
553
}

leetcode/1401-1500/1432.Max-Difference-You-Can-Get-From-Changing-an-Integer/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", 555, 888},
17+
{"TestCase2", 9, 8},
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)