The number of operations into a digital 0

Address: https://leetcode-cn.com/problems/number-of-steps-to-reduce-a-number-to-zero/

<? PHP 

/ * * 
give you a non-negative integer num, will you please return it into the number of steps needed to 0. If the current number is an even number, you need to divide it by 2; otherwise, minus 1. 
Example 1: 

Input: num = 14 
Output: 6 
Explanation: 
Step 1) 14 is even, by 2 7. 
Step 2) 7 is an odd number, minus 1 to give 6. 
Step 3) 6 is even, by 2 3. 
Step 4) 3 is an odd number, minus one to give 2. 
Step 5) 2 is even, by 2 1. 
Step 6) is an odd number 1, obtained by subtracting 1 0. 
Example 2: 

Input: num = 8 
Output: 4 
explained: 
Step 1) is an even number of 8, 4 by 2. 
Step 2) 4 is an even number, by 2 2. 
Step 3) 2 is even, by 2 1. 
Step 4) is an odd number 1, obtained by subtracting 1 0. 
Example 3: 

Input: num = 123 
Output: 12 
 

Tip: 

0 <NUM = <= 10. 6 ^ 
 * / 
class Solution {
     / ** 
     * @Param integer $ num 
     * @return Integer 
     * / 
    function numberOfSteps ( $ num ) {
         $ i = 0 ;
        while ( $ num > 0 ) {
             $ i ++ ;
//             $ num = $ num% 2 == 0? $ num / 2 - $ num; 1 //方式

                $ num = $ num & 1? - $ Surely , $ whether / 2;    // 方式2 
        }
         return  $ i ; 
    } 
} 

$ Test = new solution ();
$ whether = 14;
//
echo $test->numberOfSteps($num);

 

Guess you like

Origin www.cnblogs.com/8013-cmf/p/12553675.html