Simple python

  • 0
    class Solution(object):
        def maximumSwap(self, num):
            :type num: int
            :rtype: int
            curr_max, candidate = -1, (-1,-1)
            nums = list(str(num))
            for i in xrange(-1,-len(nums)-1, -1):
                if nums[i] < nums[curr_max]:
                    candidate = (i, curr_max)
                elif nums[i] > nums[curr_max]:
                    curr_max = i      
            nums[candidate[0]],nums[candidate[1]] = nums[candidate[1]],nums[candidate[0]]
            return int(''.join(nums))

Log in to reply

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