Does the Time "Limit Exceeded" mean the performance is not well ?


  • 0
    J

    my ideas: 1 copy all the list and save org copied 2 find index that every node of random point 3 upd

      if (head == NULL)
      {
         return NULL;
      }
    
      RandomListNode * orgCurrent = head;
    
      RandomListNode* resHead = NULL;
      RandomListNode* resTail = NULL;
      vector <RandomListNode*> *orgNodeList = new vector <RandomListNode*> ();
      vector <RandomListNode*> *deepNodeList = new vector <RandomListNode*> ();;
      while (orgCurrent != NULL)
      {
         // Save the index first.
         RandomListNode* copyNode = new RandomListNode (orgCurrent->label);
         deepNodeList->push_back (copyNode);
         orgNodeList->push_back (orgCurrent);
         if (resHead == NULL)
         {
           resHead = resTail = copyNode;
         }
         else
         {
            resTail->next = copyNode;
            resTail = copyNode;
         }
         orgCurrent = orgCurrent->next;
      }
    
      vector <int> *orgRandomIndex = new vector <int> ();
    
      for(int index = 0; index < orgNodeList->size (); ++index)
      {
         if((*orgNodeList)[index]->random == NULL)
         {
            orgRandomIndex->push_back (-1);
         }
         else
         {
            bool find = false;
            for (int randomindex = 0; randomindex < orgNodeList->size (); ++randomindex)
            {
               if((*orgNodeList)[randomindex] == (*orgNodeList)[index]->random)
               {
                  find = true;
                  orgRandomIndex->push_back (randomindex);
                  break;
               }
            }
    
            if(find == false)
            {
               orgRandomIndex->push_back (-1);
            }
         }
      }
      RandomListNode* resCurrent = resHead;
      int tmpindex = 0;
      while(resCurrent != NULL)
      {
         if ((*orgRandomIndex) [tmpindex] == -1)
         {
            resCurrent->random = NULL;
         }
         else
         {
            resCurrent->random = (*deepNodeList) [(*orgRandomIndex) [tmpindex]];
         }
         tmpindex++;
         resCurrent = resCurrent->next;
      }
    
      delete orgNodeList;
      delete deepNodeList;
      delete orgRandomIndex;
    
      return resHead;
    

    }


Log in to reply
 

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