File tree Expand file tree Collapse file tree 1 file changed +13
-10
lines changed
Expand file tree Collapse file tree 1 file changed +13
-10
lines changed Original file line number Diff line number Diff line change 99public class Main5 {
1010 //dp[start][end] = dp[start+1][end-1] && s.charAt(start)==s.charAt(end)
1111 public String longestPalindrome (String s ) {
12- int len = s .length ();
13- if (s ==null || len ==0 )
12+ if (s == null || s .length () == 0 )
1413 return s ;
15- boolean dp [][] = new boolean [len ][len ];
14+ int n = s .length ();
15+ boolean [][] dp = new boolean [n ][n ];
1616 String res = "" ;
17- for (int l = 1 ; l <= len ; l ++) {
18- for (int start = 0 ; start < len ; start ++) {
19- int end = start + l - 1 ;
20- if (end >= len )
17+ for (int len = 1 ; len <= n ; len ++) {
18+ for (int start = 0 ; start < n ; start ++) {
19+ int end = start + len - 1 ;
20+ if (end >= n )
2121 break ;
22- dp [start ][end ] = l ==1 || l ==2 && s .charAt (start )==s .charAt (end ) || dp [start +1 ][end -1 ] && s .charAt (start )==s .charAt (end );
23- if (dp [start ][end ] && end -start +1 > res .length ())
24- res = s .substring (start , end +1 );
22+ dp [start ][end ] = len == 1
23+ || len == 2 && s .charAt (start ) == s .charAt (end )
24+ || dp [start +1 ][end -1 ] && s .charAt (start ) == s .charAt (end );
25+ if (dp [start ][end ] && len > res .length ()) {
26+ res = s .substring (start , end + 1 );
27+ }
2528 }
2629 }
2730 return res ;
You can’t perform that action at this time.
0 commit comments