Java O(n) 4 lines


  • 0
    T

    Using Java's Integer class:

    public class Solution {
        public int[] countBits(int num) {
            int[] result = new int[num+1];
            for (int i = 0; i <= num; ++i)
                result[i] = Integer.bitCount(i);
            return result;
        }
    }
    

Log in to reply
 

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