Python Solution


  • 0
    class Solution(object):
        def maximumSwap(self, num):
            strnum = list(str(num))
            stnum = sorted(strnum)[::-1]
            i = 0
            while i < len(stnum):
                if strnum[i] != stnum[i]:
                    tmp1,  tmp2= strnum[i], stnum[i]
                    strnum[i] = stnum[i]
                    for j in range(len(stnum) - 1, -1, -1):
                        if strnum[j] == tmp2:
                            strnum[j] = tmp1
                            return int(''.join(strnum))
                i += 1
            return int(''.join(strnum))
            
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.