java.lang.Object
g1301_1400.s1342_number_of_steps_to_reduce_a_number_to_zero.Solution

public class Solution extends Object
1342 - Number of Steps to Reduce a Number to Zero.<p>Easy</p> <p>Given an integer <code>num</code>, return <em>the number of steps to reduce it to zero</em>.</p> <p>In one step, if the current number is even, you have to divide it by <code>2</code>, otherwise, you have to subtract <code>1</code> from it.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> num = 14</p> <p><strong>Output:</strong> 6</p> <p><strong>Explanation:</strong></p> <p>Step 1) 14 is even; divide by 2 and obtain 7.</p> <p>Step 2) 7 is odd; subtract 1 and obtain 6.</p> <p>Step 3) 6 is even; divide by 2 and obtain 3.</p> <p>Step 4) 3 is odd; subtract 1 and obtain 2.</p> <p>Step 5) 2 is even; divide by 2 and obtain 1.</p> <p>Step 6) 1 is odd; subtract 1 and obtain 0.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> num = 8</p> <p><strong>Output:</strong> 4</p> <p><strong>Explanation:</strong></p> <p>Step 1) 8 is even; divide by 2 and obtain 4.</p> <p>Step 2) 4 is even; divide by 2 and obtain 2.</p> <p>Step 3) 2 is even; divide by 2 and obtain 1.</p> <p>Step 4) 1 is odd; subtract 1 and obtain 0.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> num = 123</p> <p><strong>Output:</strong> 12</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 <= num <= 10<sup>6</sup></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • numberOfSteps

      public int numberOfSteps(int num)