昨天的
先大到小排
第一个diff digit就是我们要换的
然后再从后面找过来要跟谁换
https://i.imgur.com/vutTUsK.png
早上都没人喔== 跑这么快
def maximumSwap(self, num: int) -> int:
max_s = sorted(list(str(num)), reverse=True)
target_digit, target_idx = '', -1
for i in range(len(max_s)):
if max_s[i] != str(num)[i]:
target_digit = max_s[i]
target_idx = i
break
swap_idx = -1
for i in reversed(range(len(max_s))):
if target_digit == str(num)[i]:
swap_idx = i
break
ans = list(str(num))
ans[target_idx], ans[swap_idx] = ans[swap_idx], ans[target_idx]
return int(''.join(ans))