Simple and easy understanding Java O(n)


  • 0
    D
    public int[] countBits(int num) {
        int[] answer = new int[num+1];
        for(int i = 1;i <= num; i++){
            answer[i] = answer[i/2] + i%2;
        }
        return answer;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.