Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Desolve/ac7bed87672d4c13dc8df0fb361f460d to your computer and use it in GitHub Desktop.
Save Desolve/ac7bed87672d4c13dc8df0fb361f460d to your computer and use it in GitHub Desktop.
1342 Number of Steps to Reduce a Number to Zero
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