mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-14 01:49:41 +01:00
19 lines
295 B
Go
19 lines
295 B
Go
|
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
|
||
|
}
|