Leetcode 1520 Solution

This article provides solution to leetcode question 1520 (number-of-steps-to-reduce-a-number-in-binary-representation-to-one)

https://leetcode.com/problems/number-of-steps-to-reduce-a-number-in-binary-representation-to-one

Solution

class Solution:
    def numSteps(self, s: str) -> int:
        v = 0
        for ch in s:
            v = v * 2 + (ch == '1')

        step = 0
        while v != 1:
            step += 1

            if v % 2 == 0:
                v = v // 2
            else:
                v += 1

        return step