Python Solution


  • 1
    B
    class Solution(object):
        def integerReplacement(self, n):
            steps = 0
            while n != 1:
                if n % 2 == 0:
                    n /= 2
                else:
                    x = (n-1)/2
                    if x % 2 == 0 or x == 1:
                        n -= 1
                    else:
                        n += 1
                steps += 1
            return steps
    

Log in to reply
 

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