1404. Number of Steps to Reduce a Number in Binary Representation to One
</> Solution
/**
* Problem: LeetCode 1404 - Number of Steps to Reduce a Number in Binary Representation to One
* Difficulty: Medium
* Language: Java
* Author: CodeByTushu
*/
class Solution {
public int numSteps(String s) {
int steps = 0;
int carry = 0;
for (int i = s.length() - 1; i > 0; i--) {
int bit = (s.charAt(i) - '0') + carry;
if (bit % 2 == 1) {
// Odd: add 1 (1 step) making it even, then divide by 2 (1 step) -> 2 steps total
steps += 2;
carry = 1;
} else {
// Even: divide by 2 (1 step)
steps += 1;
}
}
// At index 0, value is 1 + carry. If carry is 1, it became 2, requiring 1 more step to reduce to 1.
return steps + carry;
}
}
"""
Problem: LeetCode 1404 - Number of Steps to Reduce a Number in Binary Representation to One
Difficulty: Medium
Language: Python
Author: CodeByTushu
"""
class Solution:
def numSteps(self, s: str) -> int:
steps = 0
carry = 0
for i in range(len(s) - 1, 0, -1):
bit = (ord(s[i]) - 48) + carry
if bit % 2 == 1:
steps += 2
carry = 1
else:
steps += 1
return steps + carry
/**
* Problem: LeetCode 1404 - Number of Steps to Reduce a Number in Binary Representation to One
* Difficulty: Medium
* Language: C++
* Author: CodeByTushu
*/
#include <string>
using namespace std;
class Solution {
public:
int numSteps(string s) {
int steps = 0;
int carry = 0;
for (int i = (int)s.length() - 1; i > 0; i--) {
int bit = (s[i] - '0') + carry;
if (bit % 2 == 1) {
steps += 2;
carry = 1;
} else {
steps += 1;
}
}
return steps + carry;
}
};
/**
* Problem: LeetCode 1404 - Number of Steps to Reduce a Number in Binary Representation to One
* Difficulty: Medium
* Language: JavaScript
* Author: CodeByTushu
*/
/**
* @param {string} s
* @return {number}
*/
var numSteps = function(s) {
let steps = 0;
let carry = 0;
for (let i = s.length - 1; i > 0; i--) {
const bit = (s.charCodeAt(i) - 48) + carry;
if (bit % 2 === 1) {
steps += 2;
carry = 1;
} else {
steps += 1;
}
}
return steps + carry;
};