Number of Steps to Reduce a Number in Binary Representation to One LeetCode solution in Java

1
class Solution {
2
public int numSteps(String s) {
3
int countSteps = 0;
4
int carry = 0;
5
for (int i = s.length() - 1; i >= 1; i--) {
6
int rightMostBit = s.charAt(i) - '0';
7
if ((rightMostBit + carry) == 1) {
8
carry = 1;
9
countSteps += 2;
10
} else {
11
countSteps++;
12
}
13
}
14
return countSteps + carry;
15
}
16
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0