【Leetcode】No.5 Longest Palindromic Substring
作者:互联网
一、暴力法
public String longestPalindrome(String s) {
//暴力算法
if(s.isEmpty()){
return s;
}
String res = s.substring(0,1);
for (int i =0; i<s.length();i++){
for (int j=i+1;j<=s.length();j++){ //此处==是因为子串只取到前一位
String os = s.substring(i,j); //子串正序
String rs = new StringBuffer(os).reverse().toString(); //子串逆序
if (os.equals(rs) && os.length()>res.length()){
res = os;
}
}
}
return res;
}
二、动态规划
标签:substring,return,String,longestPalindrome,Palindromic,res,Substring,No.5,暴力 来源: https://blog.csdn.net/muyimo/article/details/89450033