https://leetcode.com/problems/maximum-swap
670. Maximum Swap
给一个整数num 最多可以交换一次2位数字以获得最大值
回传可以获得的最大值
Example 1:
  Input: num = 2736
  Output: 7236
  Explanation: 交换2跟7
Example 2:
  Input: num = 9973
  Output: 9973
  Explanation: 不用交换
Constraints:
  0 <= num <= 10^8
思路:
原本很单纯右起直接找最大值 再左起找能交换的
结果在测资 98368 就错了
改为先排序 判断从哪个位置开始不同 再找出原始位置并交换
因为 str 不能直接交换两个位置 所以转成 list 交换后再 join
Python Code:
class Solution:
    def maximumSwap(self, num: int) -> int:
        num_str = str(num)
        num_list = list(num_str)
        num_sorted = sorted(num_list, reverse=True)
        if num_list == num_sorted: return num
        for i, temp in enumerate(num_sorted):
            if temp != num_list[i]:
                idx = num_str.rindex(temp)
                num_list[i], num_list[idx] = num_list[idx], num_list[i]
                return int(''.join(num_list))
        return num
怪怪的怪怪的