From 413ce7c4ab6564294ef811a3f667295f8a019403 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sun, 15 Jun 2025 20:34:13 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.1432 No.1432.Max Difference You Can Get From Changing an Integer --- .../README.md | 65 ++++++++++++++++- .../README_EN.md | 71 ++++++++++++++++++- .../Solution.rs | 28 ++++++++ .../Solution.ts | 21 ++++++ 4 files changed, 181 insertions(+), 4 deletions(-) create mode 100644 solution/1400-1499/1432.Max Difference You Can Get From Changing an Integer/Solution.rs create mode 100644 solution/1400-1499/1432.Max Difference You Can Get From Changing an Integer/Solution.ts diff --git a/solution/1400-1499/1432.Max Difference You Can Get From Changing an Integer/README.md b/solution/1400-1499/1432.Max Difference You Can Get From Changing an Integer/README.md index 2d6da8186663c..4c329fbbdcd4b 100644 --- a/solution/1400-1499/1432.Max Difference You Can Get From Changing an Integer/README.md +++ b/solution/1400-1499/1432.Max Difference You Can Get From Changing an Integer/README.md @@ -94,13 +94,13 @@ tags: 要想得到最大差值,那么我们应该拿到最大值与最小值,这样差值最大。 -因此,我们先从高到低枚举 $nums$ 每个位置上的数,如果数字不为 `9`,就将所有该数字替换为 `9`,得到最大整数 $a$。 +因此,我们先从高到低枚举 $\textit{nums}$ 每个位置上的数,如果数字不为 `9`,就将所有该数字替换为 `9`,得到最大整数 $a$。 -接下来,我们再从高到低枚举 `nums` 每个位置上的数,首位不能为 `0`,因此如果首位不为 `1`,我们将其替换为 `1`;如果非首位,且数字不与首位相同,我们将其替换为 `0`,得到最大整数 $b$。 +接下来,我们再从高到低枚举 $\textit{nums}$ 每个位置上的数,首位不能为 `0`,因此如果首位不为 `1`,我们将其替换为 `1`;如果非首位,且数字不与首位相同,我们将其替换为 `0`,得到最大整数 $b$。 答案为差值 $a - b$。 -时间复杂度 $O(\log num)$,空间复杂度 $O(\log num)$。其中 $num$ 为给定整数。 +时间复杂度 $O(\log \textit{num})$,空间复杂度 $O(\log \textit{num})$。其中 $\textit{nums}$ 为给定整数。 @@ -214,6 +214,65 @@ func maxDiff(num int) int { } ``` +#### TypeScript + +```ts +function maxDiff(num: number): number { + let a = num.toString(); + let b = a; + for (let i = 0; i < a.length; ++i) { + if (a[i] !== '9') { + a = a.split(a[i]).join('9'); + break; + } + } + if (b[0] !== '1') { + b = b.split(b[0]).join('1'); + } else { + for (let i = 1; i < b.length; ++i) { + if (b[i] !== '0' && b[i] !== '1') { + b = b.split(b[i]).join('0'); + break; + } + } + } + return +a - +b; +} +``` + +#### Rust + +```rust +impl Solution { + pub fn max_diff(num: i32) -> i32 { + let a = num.to_string(); + let mut a = a.clone(); + let mut b = a.clone(); + + for c in a.chars() { + if c != '9' { + a = a.replace(c, "9"); + break; + } + } + + let chars: Vec = b.chars().collect(); + if chars[0] != '1' { + b = b.replace(chars[0], "1"); + } else { + for &c in &chars[1..] { + if c != '0' && c != '1' { + b = b.replace(c, "0"); + break; + } + } + } + + a.parse::().unwrap() - b.parse::().unwrap() + } +} +``` + diff --git a/solution/1400-1499/1432.Max Difference You Can Get From Changing an Integer/README_EN.md b/solution/1400-1499/1432.Max Difference You Can Get From Changing an Integer/README_EN.md index d31d01cb1c408..cae87b2201938 100644 --- a/solution/1400-1499/1432.Max Difference You Can Get From Changing an Integer/README_EN.md +++ b/solution/1400-1499/1432.Max Difference You Can Get From Changing an Integer/README_EN.md @@ -67,7 +67,17 @@ We have now a = 9 and b = 1 and max difference = 8 -### Solution 1 +### Solution 1: Greedy + +To obtain the maximum difference, we should take the maximum and minimum values, as this yields the largest difference. + +Therefore, we first enumerate each digit in $\textit{nums}$ from high to low. If a digit is not `9`, we replace all occurrences of that digit with `9` to obtain the maximum integer $a$. + +Next, we enumerate each digit in $\textit{nums}$ from high to low again. The first digit cannot be `0`, so if the first digit is not `1`, we replace it with `1`; for non-leading digits that are different from the first digit, we replace them with `0` to obtain the minimum integer $b$. + +The answer is the difference $a - b$. + +The time complexity is $O(\log \textit{num})$, and the space complexity is $O(\log \textit{num})$, where $\textit{nums}$ is the given integer. @@ -181,6 +191,65 @@ func maxDiff(num int) int { } ``` +#### TypeScript + +```ts +function maxDiff(num: number): number { + let a = num.toString(); + let b = a; + for (let i = 0; i < a.length; ++i) { + if (a[i] !== '9') { + a = a.split(a[i]).join('9'); + break; + } + } + if (b[0] !== '1') { + b = b.split(b[0]).join('1'); + } else { + for (let i = 1; i < b.length; ++i) { + if (b[i] !== '0' && b[i] !== '1') { + b = b.split(b[i]).join('0'); + break; + } + } + } + return +a - +b; +} +``` + +#### Rust + +```rust +impl Solution { + pub fn max_diff(num: i32) -> i32 { + let a = num.to_string(); + let mut a = a.clone(); + let mut b = a.clone(); + + for c in a.chars() { + if c != '9' { + a = a.replace(c, "9"); + break; + } + } + + let chars: Vec = b.chars().collect(); + if chars[0] != '1' { + b = b.replace(chars[0], "1"); + } else { + for &c in &chars[1..] { + if c != '0' && c != '1' { + b = b.replace(c, "0"); + break; + } + } + } + + a.parse::().unwrap() - b.parse::().unwrap() + } +} +``` + diff --git a/solution/1400-1499/1432.Max Difference You Can Get From Changing an Integer/Solution.rs b/solution/1400-1499/1432.Max Difference You Can Get From Changing an Integer/Solution.rs new file mode 100644 index 0000000000000..17cbc4f0dd6bc --- /dev/null +++ b/solution/1400-1499/1432.Max Difference You Can Get From Changing an Integer/Solution.rs @@ -0,0 +1,28 @@ +impl Solution { + pub fn max_diff(num: i32) -> i32 { + let a = num.to_string(); + let mut a = a.clone(); + let mut b = a.clone(); + + for c in a.chars() { + if c != '9' { + a = a.replace(c, "9"); + break; + } + } + + let chars: Vec = b.chars().collect(); + if chars[0] != '1' { + b = b.replace(chars[0], "1"); + } else { + for &c in &chars[1..] { + if c != '0' && c != '1' { + b = b.replace(c, "0"); + break; + } + } + } + + a.parse::().unwrap() - b.parse::().unwrap() + } +} diff --git a/solution/1400-1499/1432.Max Difference You Can Get From Changing an Integer/Solution.ts b/solution/1400-1499/1432.Max Difference You Can Get From Changing an Integer/Solution.ts new file mode 100644 index 0000000000000..b83ebad41364b --- /dev/null +++ b/solution/1400-1499/1432.Max Difference You Can Get From Changing an Integer/Solution.ts @@ -0,0 +1,21 @@ +function maxDiff(num: number): number { + let a = num.toString(); + let b = a; + for (let i = 0; i < a.length; ++i) { + if (a[i] !== '9') { + a = a.split(a[i]).join('9'); + break; + } + } + if (b[0] !== '1') { + b = b.split(b[0]).join('1'); + } else { + for (let i = 1; i < b.length; ++i) { + if (b[i] !== '0' && b[i] !== '1') { + b = b.split(b[i]).join('0'); + break; + } + } + } + return +a - +b; +}