Can someone please review my solution (Posted in C++)


  • 0
    L
        int strSize = s.length();  
     19     string *tempStr = new string[numRows];  //Temp string object
     20     
     21     int row =0;                             //Push character row by row
     22     for(int i = 0; i<strSize; i++)
     23       { 
     24         tempStr[row].push_back(s[i]);       //appending string row-wise
     25         if(numRows == row+1) row = -1;
     26         row = row+1;
     27       }       
     28   
     29   s.clear();                                
     30 
     31   for(int i=0; i< numRows; i++) 
     32       s.append(tempStr[i]);
     33 
     34   return s;
    }
    

    My code is not getting completely accepted. Am I missing any rule for this code?


  • 0
    S

    Your code goes from top to bottom and then goes back to top again (row will be 0 1 2 3 4 ... n 0 1 2 3 4 ... n). It need to goes from 0 to last row then goes back up to 0 (0 1 2 3 4 ... n-1 n-2 n n-1 n-2 ... 3 2 1 0 ...).


Log in to reply
 

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