Number Of Steps To Reduce Binary Number To One
Reduce binary number to 1 in steps: if even, divide by 2; if odd, add 1. Example: "1101" -> 6 steps. Solution uses a while loop and substr to count steps.
1404. Number of Steps to Reduce a Number in Binary Representation to One Medium Given the binary representation of an integer as a string s, return the number of steps to reduce it to 1 under the following rules: If the current number is even, you have to divide it by 2. If the current number is odd, you have to add 1 to it. It is guaranteed that you can always reach one for all test cases. Example 1: Input: "1101" Output: 6 Explanation: "1101" corressponds to number 13 in their decimal representation. Step 1) 13 is odd, add 1 and obtain 14. Step 2) 14 is even, divide by 2 and obtain 7. St...
