Max Difference You Can Get From Changing an Integer LeetCode solution in Rust

1
impl Solution {
2
pub fn max_diff(num: i32) -> i32 {
3
let mut a = vec![];
4
let mut num = num;
5
while num > 9 {
6
a.push(num % 10);
7
num /= 10;
8
}
9
a.push(num);
10
a.reverse();
11

12
let mut b = a.clone();
13
let mut x = -1;
14
let mut y = (-1, 0);
15
for i in 0..a.len() {
16
if x != -1 {
17
if a[i] == x {
18
a[i] = 9;
19
}
20
} else {
21
if a[i] != 9 {
22
x = a[i];
23
a[i] = 9;
24
}
25
}
26
}
27

28
if b[0] != 1 {
29
y.0 = b[0];
30
y.1 = 1;
31
b[0] = 1;
32
}
33
for i in 1..b.len() {
34
if y.0 != -1 {
35
if b[i] == y.0 {
36
b[i] = y.1;
37
}
38
} else {
39
if b[i] > 1 {
40
y.0 = b[i];
41
b[i] = 0;
42
}
43
}
44
}
45
let mut result = 0;
46
for i in 0..a.len() - 1 {
47
result += a[i] - b[i];
48
result *= 10;
49
}
50

51
result + a.last().unwrap() - b.last().unwrap()
52
}
53
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0