A brute force method, but accepted, why ??


  • 0
    E
     public String longestPalindrome2(String s) {
    	 if(s == null ||s.length()<=0)
    		 return null;
    	 char[] c  =  s.toCharArray();
    	 for(int len = c.length ;len>1;len--){
    		 for(int start=0;start<=c.length-len;start++){
    			 int i=start,j = start+len-1;
    			 while(i<=j && c[i] == c[j])
    			{
    				 i++;
    				 j--;
    			}
    			 if(i>=j)
    				 return s.substring(start,start+len);
    		 }
    	 }
    	 return s.substring(0,1);
     }

  • 0
    N

    I guess it's because you search from the maximum length and test cases are not enough.


Log in to reply
 

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