4ms Java, bitwise operator


  • 6
    Y
    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 >> 1)] + (i&1);
            }
            return result;
        }
    }

Log in to reply
 

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