Output palindromic longest substring

Given a string s, s output palindromic longest substring

 1 class Solution {
 2     public String longestPalindrome(String s) {
 3         if(s==null||s.length()<1){
 4             return "";
 5         }
 6         int len=s.length();
 7         int l=0;
 8         int left=0;
 9         int right=0;
10         int[][] a=new int[len][len];
11         for(int i=0;i<len;i++){
12             a[i][i]=1;
13         }
14         l=1;
15         left=0;
16         right=0;
17         for(int i=0;i<len-1;i++){
18             if(s.charAt(i)==s.charAt(i+1)){
19                 a[i][i+1]=1;
20                 l=2;
21                 left=i;
22                 right=i+1;
23             }
24         } 
25         for(int i=a.length-3;i>=0;i--){
26             for(int j=i+2;j<a[0].length;j++){
27                 if(a[i+1][j-1]==1&&s.charAt(i)==s.charAt(j)){
28                     a[i][j]=1;
29                 }
30                 if(a[i][j]==1&&j-i+1>l){
31                     l=j-i+1;
32                     left=i;
33                     right=j;
34                 }
35             }
36         }     
37         return s.substring(left,right+1);
38     }
39 }

Reflection:

1, are not familiar with the dynamic programming algorithm.

2, where s is not considered empty.

3, when the dynamic programming algorithm fill in a form, fill in the path of the lack of thinking.

4, two-digit unskilled group definition.

5, this question optimal algorithm is the center of expansion algorithm.

Guess you like

Origin www.cnblogs.com/xiaoyaomianbiren/p/12175596.html