2ms, Java , 4 lines code


  • 2
    R

    public class Solution {
    public int[] countBits(int num) {
    int[] nums = new int[num + 1];
    for (int i = 1; i <= num; i++)
    nums[i] = nums[i >> 1] + (i & 1);
    return nums;
    }
    }


Log in to reply
 

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