3ms Java Solution easy to understand!


  • -1
    Z

    public class Solution {
    public int[] countBits(int num) {
    int[] nums = new int[num+1];
    nums[0]=0;
    int t=1;
    while(t<=num){
    nums[t]=1;
    t*=2;
    }
    int index=2;
    for(int j=2;j<num+1;j++){
    if(nums[j]!=0) {
    index=j;
    continue;
    }
    nums[j]=nums[j-index]+1;
    }
    return nums;
    }

    }


Log in to reply
 

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