Java 3ms Counting Bits


  • 0
    J
    public class Solution {
        public int[] countBits(int num) {
            int[] result = new int[num + 1];
            result[0] = 0;
            int f = 1;
            int k = 1;
            for(int i = 1;i <= num;i++){
                result[i] = k - result[f - i];
                if(i == f){
                    f = (f + 1) * 2 - 1;
                    k++;
                }
            }
            return result;
        }
    }
    

    Status: Accepted
    Runtime: 3 ms


Log in to reply
 

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