Skip to content

Add solution and test-cases for problem 1432 #1234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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},
}

// 开始测试
Expand All @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading