LC 1342. Number of Steps to Reduce a Number to Zero

Nilanjan Deb · April 5, 2020

This is my CPP solution.

class Solution {
public:
    int numberOfSteps (int num) {
        if(num == 0)
            return num;
        int cnt = 0;
        while(num){
            if(num%2==1)
                cnt++;
            cnt++;
            num/=2;
        }
        cnt--;
        return cnt;
    }
};


Dicussion Forum