首页 > 代码库 > leetcode338

leetcode338

public class Solution {
    public int[] CountBits(int num) {
        var ary = new int[num + 1];

            for (int i = 0; i <= num; i++)
            {
                var count = 0;
                var cur = i;
                do
                {
                    var c = cur % 2;
                    if (c == 1)
                    {
                        count++;
                    }
                    cur = cur / 2;
                } while (cur != 0);

                ary[i] = count;
            }

            return ary;
    }
}

https://leetcode.com/problems/counting-bits/#/description

leetcode338