10 Lines Python


  • -1
    def maximumSwap(self, num):
        a = list(str(num))
        n = len(a)
        for i in range(n):
            curmax = float('-inf')
            for ind, val in enumerate(a[i:]):
                if curmax <= val:   indmax, curmax = ind, val
            if a[i] != curmax:
                a[i], a[i+indmax] = curmax, a[i]
                break
        return int(''.join(a))

Log in to reply
 

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