C++ recursive, memory limit exceed ,don't know how to deal with this problem?


  • 0
    D
    class Solution {
    public:
        void permute(string& list, int level, vector<string>& v){
            if(level == list.size()){
                v.push_back(list);
                return;
            }
            for(int i=level;list[i];++i){
                swap(list[level], list[i]);
                permute(list, level + 1, v);
                swap(list[level], list[i]);
            }
    }
        string largestNumber(vector<int> &num) {
             string result="",final_result="",current_result="";
             int temp;
             vector<string> v;
             if(num.empty()){
                 return final_result;
             }
             temp=num[0];
             result=to_string(temp);
             int size=num.size();
             for(int i=1;i<size;i++){
                    temp=num[i];
                    result=result+to_string(temp);
             }
             permute(result,0,v);
             
             
             size=v.size();
             final_result=v[0];
             unsigned int comp1,comp2;
             
             for(int i=1;i<size;i++){
                 
                 current_result=v[i];
                 comp1=atoi( current_result.c_str() );
                 comp2=atoi( final_result.c_str() );
                 if(comp1>comp2||comp1==comp2){
                     final_result=current_result;
                 }
                 
             
    
    
    
                 
                 
             }
             
         return final_result;
            
            
        
            
            
        }
    };

  • -1
    R

    the msg 'memory limit exceed' means yr algorithm using too much memories. u got find another way around. a more effective,more efficient way to solve this problem.


Log in to reply
 

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