go: add «1404. Number of Steps to Reduce a Number in Binary Representation to One»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
2e2c8a758e
commit
af9de3f83b
1 changed files with 18 additions and 0 deletions
|
@ -0,0 +1,18 @@
|
|||
package number_of_steps_to_reduce_a_number_in_binary_representation_to_one
|
||||
|
||||
func numSteps(s string) int {
|
||||
steps := 0
|
||||
carry := 0
|
||||
for i := len(s) - 1; i > 0; i-- {
|
||||
bit := int(s[i]) + carry
|
||||
|
||||
if bit%2 == 1 {
|
||||
steps += 2
|
||||
carry = 1
|
||||
} else {
|
||||
steps += 1
|
||||
}
|
||||
}
|
||||
|
||||
return steps + carry
|
||||
}
|
Loading…
Reference in a new issue