84 ms c++ dp solution


  • 1
    O
    class Solution {
    public:
        vector<int> countBits(int num) {
            vector<int> counts(num + 1);
            counts[0] = 0;
            for (int i = 1; i <= num; i ++) {
                int base = (i - 1) & i;
                counts[i] = (base == 0) ? 1 : (counts[base] + counts[i - base]);
            }
            return counts;
        }
    };
    

    will explain later


Log in to reply
 

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