My JAVA Solution easy to understand


  • 0
    L
    public class Solution {
        public int[] countBits(int num) {
            int[] result = new int[num+1];
            for(int i = 1; i <= num; i++) {
                if(i % 2 == 1) {
                    result[i] = result[i>>1] + 1;
                } else {
                    result[i] = result[i>>1];
                }
            }
            return result;
        }
    }
    

Log in to reply
 

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