Skip to content

Commit f5ca774

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents 140b656 + c0daa0a commit f5ca774

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/main/java/N1_100/N5.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package N1_100;
2+
3+
public class N5 {
4+
public String longestPalindrome(String s) {
5+
if (s == null || s.length() < 1) {
6+
return "";
7+
}
8+
int start = 0, end = 0;
9+
for (int i = 0; i < s.length(); i++) {
10+
int len1 = expandAroundCenter(s, i, i);
11+
int len2 = expandAroundCenter(s, i, i+1);
12+
int len = Math.max(len1, len2);
13+
if (len > end - start) {
14+
start = i - (len - 1) / 2;
15+
end = i + len / 2;
16+
}
17+
}
18+
return s.substring(start, end + 1);
19+
}
20+
21+
private int expandAroundCenter(String s, int left, int right) {
22+
int L = left, R = right;
23+
while (L >= 0 && R < s.length() && s.charAt(L) == s.charAt(R)) {
24+
L--;
25+
R++;
26+
}
27+
return R - L - 1;
28+
}
29+
}

0 commit comments

Comments
 (0)