Find all stepping numbers in range N to M


  • 0
    N

    Given N and M find all stepping numbers in range N to M

    The stepping number:

    A number is called as a stepping number if the adjacent digits have a difference of 1.
    e.g 123 is stepping number, but 358 is not a stepping number

    Example:

    N = 10, M = 20
    all stepping numbers are 10 , 12
    Return the numbers in sorted order.


  • 0
    A

    My Python solution to this.

    def findSteppingNumbersOptimized(N, M):
        digits = []
    
        for x in range(N,M+1):
            copyx = x
            steppingDigit = True
            currentDigit = x%10
            x = x // 10
            nextDigit = x%10
    
            while x != 0 and steppingDigit == True:
                if abs(currentDigit -nextDigit) != 1:
                    steppingDigit = False
    
                currentDigit = nextDigit
                x = x//10
                nextDigit = x%10
    
            if steppingDigit == True:
                digits.append(copyx)
    
        return digits
    

Log in to reply
 

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