Created
February 13, 2020 16:53
-
-
Save fengyitsai/97df77099b55c5b4c3e6d936e839dca5 to your computer and use it in GitHub Desktop.
1342. Number of Steps to Reduce a Number to Zero
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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