When input "aaaa...aaa",it take a lot time. why ? I use the judgement to solve it.


  • 0
    S
    string longestPalindrome(string a){
    	int first = 0;
    	int last = 0;
    	int length = a.length();
    	int i = 0;
    	int j =length-1;
    	bool find = true;
    	while(i < length)
    	{
    		for(j = length-1;j > i;j--)
    		{
    			if(a[j] == a[i]){
    				int m = i;
    				int n = j;
    				while(m < n)
    				{
    					if(a[n] != a[m]){
    						find = false;
    						break;
    					}
    					m++;n--;
    				}
    				if(find &&((last - first) < (j - i)))
    				{
    					first = i;
    					last = j;
    					break;	
    				}
    			}
    		}
    		if((last - first + 1) == (length -i))
    			break;
    		i++;
    		find = true;
    	}
    	string result = a.substr(first,last-first+1);
    	return result;
    }

Log in to reply
 

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