[3ms] My Java Solution


  • 3
    Q

    public class Solution {

    public int[] countBits(int num) {
        int[] nBits = new int[num+1];
        int offSet = 0;
        int nextMultipleOfTwo = 1;
        
        for (int i = 1; i <= num; i++) {
            if (i == nextMultipleOfTwo) {
                nBits[i] = 1;
                offSet = nextMultipleOfTwo;
                nextMultipleOfTwo = 2 * nextMultipleOfTwo;
            } else {
                nBits[i] = 1 + nBits[i-offSet];
            }
        }
        
        return nBits;
    }
    

    }


Log in to reply
 

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