Description
Given a non-negative integer, you can swap at most one pair of digits. Return the maximum valued number you can get.
Examples
Input:
num = 2736Output:
7236Explanation:
Swap 2 and 7 to get 7236.
Input:
num = 1993Output:
9913Explanation:
Swap the first digit 1 with the largest digit 9 that appears later to get the maximum value. Choosing the rightmost 9 to swap with the 1 in position 0.
Input:
num = 54321Output:
55321Explanation:
The number is already in descending order except it is possible to improve it by swapping 4 with 5. Since 5 is already at the front, swapping the 4 (position 1) with the next largest digit that comes after it, which is 5 at position 4.
Constraints
- •
0 ≤ num ≤ 10⁸