Given a non-negative integer num, return the number of steps to reduce it to zero. If the current number is even, you have to divide it by 2, otherwise, you have to subtract 1 from it.
Example 1:
Input: num = 14
Output: 6
Explanation:
Step 1) 14 is even; divide by 2 and obtain 7.
Step 2) 7 is odd; subtract 1 and obtain 6.
Step 3) 6 is even; divide by 2 and obtain 3.
Step 4) 3 is odd; subtract 1 and obtain 2.
Step 5) 2 is even; divide by 2 and obtain 1.
Step 6) 1 is odd; subtract 1 and obtain 0.
Example 2:
Input: num = 8
Output: 4
Explanation:
Step 1) 8 is even; divide by 2 and obtain 4.
Step 2) 4 is even; divide by 2 and obtain 2.
Step 3) 2 is even; divide by 2 and obtain 1.
Step 4) 1 is odd; subtract 1 and obtain 0.
publicintnumberOfSteps (int num) {if (num <1) {return0; }int count =0;while (num >0) {if (num %2==0) { num = num /2; } else { num = num -1; } count++; }return count;}// 参考答案publicintnumberOfSteps(int num) {// Get the binary for num, as a String.String binaryString =Integer.toBinaryString(num);int steps =0;// Iterate over all the bits in the binary string.for (char bit :binaryString.toCharArray()) {if (bit =='1') { // If the bit is a 1 steps = steps +2; // Then it'll take 2 to remove. } else { // bit == '0' steps = steps +1; // Then it'll take 1 to remove. } }// We need to subtract 1, because the last bit was over-counted.return steps -1;} // 还能这样写,每一位检查是否是1publicintnumberOfSteps(int num) {// We need to handle this as a special case, otherwise it'll return -1.if (num ==0) return0;int steps =0;for (int powerOfTwo =1; powerOfTwo <= num; powerOfTwo = powerOfTwo *2) {// Apply the bit mask to check if the bit at "powerOfTwo" is a 1.if ((powerOfTwo & num) !=0) { steps = steps +2; } else { steps = steps +1; } }// We need to subtract 1, because the last bit was over-counted.return steps -1;}