Check out this


  • 0
    L
        public int[] countBits(int num) {
            int[] arr = new int[num + 1];
            if(num < 1)return arr;
            arr[1] = 1;
            int ono = 0,now = 0;
            for(int i = 2; i < num + 1; i++){
                if(((i - 1) & i) == ono){
                    arr[i] = 1;
                    now = i;
                }else if(((i-1)&i) == now){
                    arr[i] = arr[now] + 1;
                }else{
                    arr[i] = arr[((i-1)&i)] + 1;
                }
                
                if(i%2 == 0)now=i;
            }
            return arr;
        }
    }

Log in to reply
 

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