A concise c++ solution without using "%"


  • 0
    L

    class Solution {

    public:

        vector<int> countBits(int num) {
    
        vector<int> rst(num + 1);
    
        rst[0] = 0;
    
        for (int i = 1; i <= num; i++) 
    
            rst[i] = rst[i >> 1] + (i & 1);
    
        return rst;
    }
    

    };


Log in to reply
 

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