美文网首页
1342. Number of steps to Reduce

1342. Number of steps to Reduce

作者: 30岁每天进步一点点 | 来源:发表于2020-02-29 20:44 被阅读0次

    附leetcode链接:https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/
    1342. Number of steps to Reduce a Number to Zero
    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.

    publice int numberOfSteps(int num) {
        int steps = 0;
        while(num > 0) {
              if(num%2 == 0)
                    num = num/2;
              else
                    num--;
              steps++;
        }
        return steps;
    }
    

    小结:对一个int类型的数字进行算数处理,用到while循环、if判断。

    相关文章

      网友评论

          本文标题:1342. Number of steps to Reduce

          本文链接:https://www.haomeiwen.com/subject/vfdvhhtx.html