blob: ce384c1570a0ef77146d90b8426cb5a469250337 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
# 670. Maximum Swap
"""
you are given an integer 'num'. you can swap two digits at most once to get
the maximum valued number. return the maximum valued number you can get.
"""
class Solution(object):
def maximumSwap(self, num):
"""
:type num: int
:rtype: int
"""
num = [int(x) for x in str(num)]
max_idx, xi, yi = len(num) - 1, 0, 0
for i in range(len(num) - 1, -1, -1):
if num[i] > num[max_idx]:
max_idx = i
elif num[i] < num[max_idx]:
xi = i
yi = max_idx
num[xi], num[yi] = num[yi], num[xi]
return int("".join([str(x) for x in num]))
if __name__ == "__main__":
obj = Solution()
print(obj.maximumSwap(2736))
print(obj.maximumSwap(9973))
|