leetcode 338. 比特位计数

思路

代码:

class Solution {
public:
    vector<int> countBits(int num) {
        vector<int>result(num+1);
        result[0] = 0;
        for(int i =1;i<=num;i++)
        {
            if(i%2 == 0){
                result[i] = result[i/2];
            }
            else
            {
                result[i] = result[i-1]+1;
            }
        }
        return result;

    }
};

 

posted @ 2021-09-26 16:03  A-inspire  Views(23)  Comments(0Edit  收藏  举报