[leetcode] Number of Steps to Reduce a Number to Zero

problem

code

    public int numberOfSteps (int num) {
        return count(num, 0);
    }

    public int count(int num, int count) {
        if (num == 0) return count;
        count++;
        if (num % 2 == 0) num /=2;
        else num--;
        return count(num, count);
    }

Time: O(N), ex) 2 => need 2 steps
Space: O(1), just need count

좋은 웹페이지 즐겨찾기