Leetcode 1520 Solution
This article provides solution to leetcode question 1520 (number-of-steps-to-reduce-a-number-in-binary-representation-to-one)
Access this page by simply typing in "lcs 1520" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
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