题解 | #最大数字交换#
最大数字交换
https://www.nowcoder.com/practice/ffcd59fbf5814d2e94c0e2f4a679473c
这题看着简单,其实还挺不容易的,牛客的用例不全,有些错的代码也让通过。。。
class Solution: def maximumSwap(self, num: str) -> str: num = list(num) n = len(num) for i in range(n): index = i for j in range(n-1,i,-1): if num[j] > num[index]: index = j if index!=i: num[i], num[index] = num[index],num[i] break return ''.join(num)