Created
February 10, 2020 18:36
-
-
Save Desolve/ac7bed87672d4c13dc8df0fb361f460d to your computer and use it in GitHub Desktop.
1342 Number of Steps to Reduce a Number to Zero
This file contains 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 { | |
public int numberOfSteps (int num) { | |
return Integer.bitCount(num) + Integer.toBinaryString(num).length() - 1; | |
} | |
} | |
/* from leetcode | |
class Solution { | |
public int numberOfSteps (int num) { | |
int steps = 0; | |
while (num > 0) { | |
if (num % 2 == 0) { | |
num /= 2; | |
} else { | |
num -= 1; | |
} | |
steps++; | |
} | |
return steps; | |
} | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment