Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.
思路分析
最长回文最简单最直观的思路就是:
从某个中点(middle)向两边扫描之道不是回文
中点紧邻的两个点记为left和right
left 和 right有可能相等(字符串长度为偶数时)
直到不是回文的时候将该轮循环的子串和上轮做比较取较长者
遍历所有的可能性—O(n^2)
代码
java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
publicclassSolution005{
private String longestPalindrome(String s, int left, int right){
while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) {