My bits[i] = bits[(i - 1) & i] + 1 very simple java solution


  • 1
    J
    public class Solution {
        public int[] countBits(int num) {
            int[] bits = new int[num + 1];
            bits[0] = 0;
            for(int i = 1; i <= num; i++){
                bits[i] = bits[(i - 1) & i] + 1;
            }
            return bits;
        }
    }

Log in to reply
 

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