Simple Java solution


  • 1
    Y
    public class Solution {
        public int[] countBits(int num) {
            int[] res = new int[num + 1];
            boolean even = true;
            for (int i = 0; i <= num; i++) {
                if (even) {
                    res[i] = res[i / 2];
                    even = false;
                }
                else {
                    even = true;
                    res[i] = res[i / 2] + 1;
                }
            }
            return res;
        }
    }

Log in to reply
 

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