Maximum Swap
Input:
2736
Output:
7236
Explanation:
Swap the number 2 and the number 7.Input:
9973
Output:
9973
Explanation:
No swap.Last updated
Input:
2736
Output:
7236
Explanation:
Swap the number 2 and the number 7.Input:
9973
Output:
9973
Explanation:
No swap.Last updated
class Solution:
def maximumSwap(self,num):
"""
:type num: int
:rtype: int
"""
nums = list(map(int, str(num)))
sorted_nums = sorted(nums, reverse=True)
n = len(nums)
for i in range(n):
if nums[i] < sorted_nums[i]:
idx = n - 1 - nums[::-1].index(sorted_nums[i])
nums[idx], nums[i] = nums[i], nums[idx]
break;
return int(''.join(map(str, nums)))