Sunday, February 14, 2021

Leetcode Everyday: 1342. Number of Steps to Reduce a Number to Zero. Easy

public class Solution {
    public int NumberOfSteps (int num) {
        int steps = 0;
        while (num > 0) {
            ++steps;
            num = num % 2 == 0 ? num / 2 : num - 1;
        }
        return steps;
    }
}
Source: https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/

No comments:

Post a Comment