Java O(n) solution using &


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

Log in to reply
 

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