Oh,Always Time Limit Exceeded,plz help me out.


  • 0
    S
     public static String longestPalindrome(String s) {
    		 if(s==null||s.isEmpty()||s.length()==1)
    			 return s;
    		 int max=0;
    		 char[] arr=s.toCharArray();
    		 String result="";
    		 for(int i=0;i<s.length()-1&&s.length()-i>=max;i++) {
    			int cur=i;
    			 for(int k=arr.length-1;k>cur;k--) {
    				 System.out.println(i+"    "+k);
    				 if(arr[k]==s.charAt(i))
    				 {
    					 String p=s.substring(i, k+1);
    					 StringBuffer sb=new StringBuffer(p);
    					 if(p.equals(sb.reverse().toString())&&max<=p.length())
    					 {
    						 result=p;
    						 max=p.length();	
    						 if(max==s.length())
    							return result;
    					 }
    				 }
    			 } 
    		 }
    		 if(result.isEmpty())
    			 result=s.charAt(0)+"";
    		 return result;
    	 }
    

    I am stuck right here...83th


Log in to reply
 

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