Public classSolution { Publicstring Longestpalindrome (string s) {//The subject is the dynamic programming idea, constructs an array pal[i][j], indicates whether from I to J is a palindrome,//pal[i][j]=true;if i=j; //Pal[i][j]=true as s[i]==s[j] and j-i=1;//Pal[i][j]=true as j>i+1,s[i]==s[j] and pal[i+1][j-1]=true; intStart=0; intEnd=0; intMaxlen=0; intlen=s.length (); Boolean[] pal=New Boolean[Len][len]; for(inti=0;i<len;i++){ for(intj=0;j<len;j++) {Pal[i][j]=false; } } for(intj=0;j<len;j++){ //pal[j][j]=true; for(inti=0;i<=j;i++){ /*if (i==j-1) Pal[i][j]=s.charat (i) ==s.charat (j); if (i<j-1) {if (S.charat (i) ==s.charat (j) &&pal[i+1][j-1]) pal[i][j]=true; }*/Pal[i][j]= (S.charat (i) ==s.charat (j) && ((j-i<2) | | PAL[I+1][J-1]));//Special Introduction to the second judgment, using && | | Short Circuit Rules if(pal[i][j]&&maxlen<j-i+1) {MaxLen=j-i+1; Start=i; End=J; } } } returnS.substring (start,end+1); }}
[Leedcode 5] Longest palindromic Substring