How can I decrease the running time?


  • 0
    S
    int length = s.length();
    	string big_string("");
    	string temp("");
    	int this_length = 0,pre_length = 0;
    	if (length == 1)
    		temp = s;
    	else 
    
    	{
    	
    		for ( int i=0;i<length-1;i++)
    
    		{              
    						if (s.at(i) == s.at(i+1))
    						{       int j;
    								for ( j=1;i-j>=0&&i+j<=length-2;j++)			    
    								   if (s.at(i-j) != s.at(i+j+1))							 							  									 
    											break ;	
    
    									   this_length = 2*j;
    									   pre_length = this_length>pre_length? this_length:pre_length; 
    									   if  (pre_length <= this_length)
    									   {
    											temp.clear();
    											temp.append(s,i-j+1,2*j);
    									   }
    						}
    
              
    						 if (i<length-2 && s.at(i) == s.at(i+2))
    						      {     int j;
    									for (j=1;i-j>=0&&i+j<=length-3;j++)			    
    									   if (s.at(i-j) != s.at(i+j+2))								
    												break ;	
    
    										   this_length = 2*j+1;
    										   pre_length = this_length>pre_length? this_length:pre_length; 
    										   if  (pre_length <= this_length)
    										   {
    												temp.clear();
    												temp.append(s,i-j+1,2*j+1);
    										   }
    						     }
    	
           }
    	}
      return   temp;

Log in to reply
 

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