1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-11-10 00:09:06 +01:00
LeetCode/go/number-of-steps-to-reduce-a-number-in-binary-representation-to-one.go

19 lines
233 B
Go
Raw Normal View History

package main
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
}