diff --git a/leetcode/2501-2600/2566.Maximum-Difference-by-Remapping-a-Digit/README.md b/leetcode/2501-2600/2566.Maximum-Difference-by-Remapping-a-Digit/README.md new file mode 100644 index 000000000..a6ae58698 --- /dev/null +++ b/leetcode/2501-2600/2566.Maximum-Difference-by-Remapping-a-Digit/README.md @@ -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 diff --git a/leetcode/2501-2600/2566.Maximum-Difference-by-Remapping-a-Digit/Solution.go b/leetcode/2501-2600/2566.Maximum-Difference-by-Remapping-a-Digit/Solution.go index d115ccf5e..6138f4e0b 100755 --- a/leetcode/2501-2600/2566.Maximum-Difference-by-Remapping-a-Digit/Solution.go +++ b/leetcode/2501-2600/2566.Maximum-Difference-by-Remapping-a-Digit/Solution.go @@ -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 } diff --git a/leetcode/2501-2600/2566.Maximum-Difference-by-Remapping-a-Digit/Solution_test.go b/leetcode/2501-2600/2566.Maximum-Difference-by-Remapping-a-Digit/Solution_test.go index 14ff50eb4..b38731089 100755 --- a/leetcode/2501-2600/2566.Maximum-Difference-by-Remapping-a-Digit/Solution_test.go +++ b/leetcode/2501-2600/2566.Maximum-Difference-by-Remapping-a-Digit/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", 11891, 99009}, + {"TestCase2", 90, 99}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }