why my c++ code got Memory Limit Exceeded


  • 0
    Y

    Anyone has idea about why my code got Memory Limit Exceeded?

    class Solution {
    public:
        vector<int> countBits(int num) {
            vector<int> result;
            int k=0;
            int temp=num;
            int j;
            while(temp!=0)
            {
                temp/=2;
                k++;
            }
            k--;
            int i;
            result.push_back(0);
            if(num==0)
            return result;
            for(i=0;i<k;i++)
            {
                for(j=0;j<result.size();j++)
                {
                    result.push_back(result[j]+1);
                }
            }
            for(i=pow(2,k);i<=num;i++)
            {
                result.push_back(result[i-pow(2,k)]);
            }
            return result;
        }
    };
    

Log in to reply
 

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