1342. Number of Steps to Reduce a Number to Zero
🟩 Easy
Solution
My Solution
func numberOfSteps(num int) int {
if num == 0 {
return 0
}
result := 0
for num > 0 {
if num&1 == 1 {
result += 2
} else {
result += 1
}
num >>= 1
}
return result - 1
}

Leetcode: link
Previous1299. Replace Elements with Greatest Element on Right SideNext1346. Check If N and Its Double Exist
Last updated
Was this helpful?