diff --git a/leetcode/1401-1500/1432.Max-Difference-You-Can-Get-From-Changing-an-Integer/README.md b/leetcode/1401-1500/1432.Max-Difference-You-Can-Get-From-Changing-an-Integer/README.md new file mode 100644 index 000000000..6accc5a52 --- /dev/null +++ b/leetcode/1401-1500/1432.Max-Difference-You-Can-Get-From-Changing-an-Integer/README.md @@ -0,0 +1,42 @@ +# [1432.Max Difference You Can Get From Changing an Integer][title] + +## Description + +You are given an integer `num`. You will apply the following steps to num **two** separate times: + +- Pick a digit `x` `(0 <= x <= 9)`. +- Pick another digit `y` `(0 <= y <= 9)`. Note `y` can be equal to `x`. +- Replace all the occurrences of `x` in the decimal representation of `num` by `y`. + +Let `a` and `b` be the two results from applying the operation to `num` independently. + +Return the max difference between `a` and `b`. + +Note that neither `a` nor `b` may have any leading zeros, and **must not** be 0. + +**Example 1:** + +``` +Input: num = 555 +Output: 888 +Explanation: The first time pick x = 5 and y = 9 and store the new integer in a. +The second time pick x = 5 and y = 1 and store the new integer in b. +We have now a = 999 and b = 111 and max difference = 888 +``` + +**Example 2:** + +``` +Input: num = 9 +Output: 8 +Explanation: The first time pick x = 9 and y = 9 and store the new integer in a. +The second time pick x = 9 and y = 1 and store the new integer in b. +We have now a = 9 and b = 1 and max difference = 8 +``` + +## 结语 + +如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] + +[title]: https://leetcode.com/problems/max-difference-you-can-get-from-changing-an-integer/ +[me]: https://github.com/kylesliu/awesome-golang-algorithm diff --git a/leetcode/1401-1500/1432.Max-Difference-You-Can-Get-From-Changing-an-Integer/Solution.go b/leetcode/1401-1500/1432.Max-Difference-You-Can-Get-From-Changing-an-Integer/Solution.go index d115ccf5e..9e4579249 100755 --- a/leetcode/1401-1500/1432.Max-Difference-You-Can-Get-From-Changing-an-Integer/Solution.go +++ b/leetcode/1401-1500/1432.Max-Difference-You-Can-Get-From-Changing-an-Integer/Solution.go @@ -1,5 +1,53 @@ 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 + } + cp := make([]int, len(digits)) + copy(cp, digits) + + i := len(digits) - 1 + remap := -1 + for ; i >= 0; i-- { + if cp[i] != 9 { + remap = cp[i] + break + } + } + for ; i >= 0; i-- { + if cp[i] == remap { + cp[i] = 9 + } + } + + i = len(digits) - 1 + target := 1 + if digits[i] == 1 { + target = 0 + } + + remap = -1 + for ; i >= 0; i-- { + if digits[i] == 0 { + continue + } + if digits[i] != 1 { + remap = digits[i] + break + } + } + for ; i >= 0; i-- { + if digits[i] == remap { + digits[i] = target + } + } + a, b := 0, 0 + for i := len(digits) - 1; i >= 0; i-- { + a = a*10 + cp[i] + b = b*10 + digits[i] + } + return a - b } diff --git a/leetcode/1401-1500/1432.Max-Difference-You-Can-Get-From-Changing-an-Integer/Solution_test.go b/leetcode/1401-1500/1432.Max-Difference-You-Can-Get-From-Changing-an-Integer/Solution_test.go index 14ff50eb4..03cd71454 100755 --- a/leetcode/1401-1500/1432.Max-Difference-You-Can-Get-From-Changing-an-Integer/Solution_test.go +++ b/leetcode/1401-1500/1432.Max-Difference-You-Can-Get-From-Changing-an-Integer/Solution_test.go @@ -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", 555, 888}, + {"TestCase2", 9, 8}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }