Next permutation solution with python


  • 0
    Q
    class Solution(object):
        def nextGreaterElement(self, n):
            s = list(str(n))
            for i in range(len(s) - 1, 0, -1):
                if s[i-1] < s[i]:
                    s[i:len(s)] = reversed(s[i:len(s)])
                    t = bisect.bisect_right(s, s[i-1], i, len(s))
                    s[i-1], s[t] = s[t], s[i-1]
                    break
            nn = int("".join(s))
            return nn if nn > n and nn < (1 << 31) else -1
    

Log in to reply
 

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