Next Greater Element III


  • 0

    Click here to see the full article post


  • 0
    B

    while (j >= 0 && a[j] <= a[i]) {
    j--;
    }
    can be changed to:
    while (a[j] <= a[i]) {
    j--;
    }


  • 0
    class Solution(object):
        def nextGreaterElement(self, n):
            """
            :type n: int
            :rtype: int
            """
            a = list(str(n))
            i = len(a) - 2
            while i >= 0 and a[i] >= a[i+1]:
                i -= 1
            if i < 0:
                return -1
            j = len(a) - 1
            while j >= 0 and a[j] <= a[i]:
                j -= 1
            a[i], a[j] = a[j], a[i]
            r = a[:i+1] + a[len(a)-1:i:-1]
            res = int(''.join(r))
            return res if res < 2**31-1 else -1
    

Log in to reply
 

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