-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path670MaximumSwap.py
More file actions
34 lines (34 loc) · 1012 Bytes
/
Copy path670MaximumSwap.py
File metadata and controls
34 lines (34 loc) · 1012 Bytes
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
31
32
33
34
class Solution:
def maximumSwap(self, num):
"""
:type num: int
:rtype: int
"""
res=list(str(num))
back=[res[len(res)-1]]
for i in range(len(res)-2,-1,-1):
if res[i]>=back[len(res)-2-i]:
back.append(res[i])
else:
back.append(back[len(res)-2-i])
back.reverse()
for i in range(len(res)-1):
if back[i]==res[i]:
continue
else:
for j in range(i+1,len(res)):
if back[j]!=back[i]:
res[i],res[j-1]=res[j-1],res[i]
break
elif j==len(res)-1:
res[i],res[j]=res[j],res[i]
break
break
return int(''.join(res))
def main():
num=2736
solution=Solution()
result=solution.maximumSwap(num)
print(result)
if __name__=='__main__':
main()