670. Maximum Swap

Given a non-negative integer, you could swap two digits at most once to get the maximum valued number. Return the maximum valued number you could get.

Example 1:

Input: 2736
Output: 7236
Explanation: Swap the number 2 and the number 7.

Example 2:

Input: 9973
Output: 9973
Explanation: No swap.

Note:

  1. The given number is in the range [0, 108]

Rust Solution

struct Solution;

impl Solution {
    fn maximum_swap(num: i32) -> i32 {
        let mut s: Vec<char> = num.to_string().chars().collect();
        let n = s.len();
        let mut last: Vec<usize> = vec![0; 10];
        for i in 0..n {
            last[(s[i] as u8 - b'0') as usize] = i;
        }
        for i in 0..n {
            let d = (s[i] as u8 - b'0') as usize;
            for j in (d + 1..10).rev() {
                if last[j] > i {
                    s.swap(i, last[j]);
                    return s.into_iter().collect::<String>().parse::<i32>().unwrap();
                }
            }
        }
        num
    }
}

#[test]
fn test() {
    let num = 2736;
    let res = 7236;
    assert_eq!(Solution::maximum_swap(num), res);
    let num = 9973;
    let res = 9973;
    assert_eq!(Solution::maximum_swap(num), res);
    let num = 9973;
    let res = 9973;
    assert_eq!(Solution::maximum_swap(num), res);
    let num = 19;
    let res = 91;
    assert_eq!(Solution::maximum_swap(num), res);
}

Having problems with this solution? Click here to submit an issue on github.