My Java AC solution


  • 1
    public class Solution {
        public int[] countBits(int num) {
            // DP
            int[] result = new int[num+1];
            int offset = 1;
            for (int i = 1; i <= num; i ++){
                if (i >= offset * 2){
                    offset *= 2;
                }
                result[i] = result[i - offset] + 1;
            }
            return result;
        }
    // Time: O(n), Space: O(1) exclude output array
    }
    

Log in to reply
 

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