File tree Expand file tree Collapse file tree 3 files changed +98
-9
lines changed
leetcode/1401-1500/1432.Max-Difference-You-Can-Get-From-Changing-an-Integer Expand file tree Collapse file tree 3 files changed +98
-9
lines changed Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change 1
1
package Solution
2
2
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
5
53
}
Original file line number Diff line number Diff line change @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
10
10
// 测试用例
11
11
cases := []struct {
12
12
name string
13
- inputs bool
14
- expect bool
13
+ inputs int
14
+ expect int
15
15
}{
16
- {"TestCase" , true , true },
17
- {"TestCase" , true , true },
18
- {"TestCase" , false , false },
16
+ {"TestCase1" , 555 , 888 },
17
+ {"TestCase2" , 9 , 8 },
19
18
}
20
19
21
20
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
30
29
}
31
30
}
32
31
33
- // 压力测试
32
+ // 压力测试
34
33
func BenchmarkSolution (b * testing.B ) {
35
34
}
36
35
37
- // 使用案列
36
+ // 使用案列
38
37
func ExampleSolution () {
39
38
}
You can’t perform that action at this time.
0 commit comments