Skip to content

Instantly share code, notes, and snippets.

@fengyitsai
Created February 13, 2020 16:53
Show Gist options
  • Save fengyitsai/97df77099b55c5b4c3e6d936e839dca5 to your computer and use it in GitHub Desktop.
Save fengyitsai/97df77099b55c5b4c3e6d936e839dca5 to your computer and use it in GitHub Desktop.
1342. Number of Steps to Reduce a Number to Zero
class Solution {
func numberOfSteps (_ num: Int) -> Int {
var num = num
var counter = 0
while num > 0 {
if num % 2 == 0 {
num = num/2
counter += 1
} else {
num = num - 1
counter += 1
}
}
return counter
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment