Max Difference You Can Get From Changing an Integer LeetCode solution in Rust
1impl Solution {2pub fn max_diff(num: i32) -> i32 {3let mut a = vec![];4let mut num = num;5while num > 9 {6a.push(num % 10);7num /= 10;8}9a.push(num);10a.reverse();1112let mut b = a.clone();13let mut x = -1;14let mut y = (-1, 0);15for i in 0..a.len() {16if x != -1 {17if a[i] == x {18a[i] = 9;19}20} else {21if a[i] != 9 {22x = a[i];23a[i] = 9;24}25}26}2728if b[0] != 1 {29y.0 = b[0];30y.1 = 1;31b[0] = 1;32}33for i in 1..b.len() {34if y.0 != -1 {35if b[i] == y.0 {36b[i] = y.1;37}38} else {39if b[i] > 1 {40y.0 = b[i];41b[i] = 0;42}43}44}45let mut result = 0;46for i in 0..a.len() - 1 {47result += a[i] - b[i];48result *= 10;49}5051result + a.last().unwrap() - b.last().unwrap()52}53}