描述:Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.(求最大的回文数)
Example 1:
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Example 2:
Input: "cbbd"
Output: "bb"
public class LongestPalindrome {
public static void main(String[] args) {
String str ="abckdllsdb";
long start = System.nanoTime();
String res = longestPalindrome(str);
System.out.println(System.nanoTime()-start);
System.out.println(res);
}
public static String longestPalindrome(String str){
int length = str.length();
int count = 0;
int star =0;
for (int i = 0; i <length ; i++) {
char c = str.charAt(i);
for (int j = i+1; j < length; j++) {
if (c==str.charAt(j) && j-i >count ) {
count =j-i;
star =i;
}
}
}
return str.substring(star,star+count+1);
}
}