Search topics...

Maximum Swap

medium
arrayTime: O(n)Space: O(1)Frequency: 4

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

Example 1:

Input: num = 2736
Output: 7236
Explanation: Swap digits 2 and 7.

Example 2:

Input: num = 9973
Output: 9973
Explanation: No swap needed — already the maximum.