LeetCode 670. [Python] Maximum Swap
原题链接
中等
作者:
徐辰潇
,
2020-06-20 08:39:06
,
所有人可见
,
阅读 767
class Solution:
def maximumSwap(self, num: int) -> int:
#TC: O(len(num))
#SC: O(1)
List = [ele for ele in str(num)]
Bucket = [-1] * 10 #the last position of the appearance of 0-9
for i in range(len(List)):
Bucket[int(List[i])] = i
for i in range(len(List)):
ele = int(List[i])
for j in range(9, ele, -1):
if Bucket[j] > i:
List[i] = str(j)
List[Bucket[j]] = str(ele)
res = int(''.join(List))
return res
return num