我正在浏览longest palindrome program:
public static String longestPalindrome(String s) {
if(s==null || s.length()<=1)
return s;
int len = s.length();
int maxLen = 1;
boolean [][] dp = new boolean[len][len];
String longest = null;
for(int k=0; k<s.length(); k++){
for(int i=0; i<len-k; i++){
int j = i+k;
if(s.charAt(i)==s.charAt(j) && (j-i<=2||dp[i+1][j-1])){
dp[i][j]=true;
if(j-i+1>maxLen){
maxLen = j-i+1;
longest = s.substring(i, j+1);
}
}
}
}
return longest;
}
我在我的eclipse中多次以调试模式运行这个程序,但我不清楚这个程序如何能够获得最长的palidrome值。
如何使用布尔数组,如何使用变量j
,主要是使用这个条件j-i<=2||dp[i+1][j-1]
该链接表示空间复杂度为O(n^2)
,该程序的哪一部分表明了这种空间复杂性。
答案 0 :(得分:1)
这是一种动态计划方法。从一个字母字符串(默认为回文)开始,逐渐增加字符串中的字母数。
为此他们正在使用二维数组,
/*
* B A N A N A
* ------------------
* B | T F F F F F
* A | T F T F T
* N | T F T F
* A | T F T
* N | T F
* A | T
* */
因此空间复杂性n ^ 2.
对于任何[i] [j]单元格,您需要检查[i + 1] [j-1]单元格,以便在添加此字母之前查明子字符串是否为回文。
以下链接有一个非常清晰的视频它正在使用类似的方法 http://www.ideserve.co.in/learn/longest-palindromic-substring
答案 1 :(得分:0)
const lpal = str => {
let lpal = ""; // to store longest palindrome encountered
let pal = ""; // to store new palindromes found
let left; // to iterate through left side indices of the character considered to be center of palindrome
let right; // to iterate through left side indices of the character considered to be center of palindrome
let j; // to iterate through all characters and considering each to be center of palindrome
for (let i=0; i<str.length; i++) { // run through all characters considering them center of palindrome
pal = str[i]; // initializing current palindrome
j = i; // setting j as index at the center of palindorme
left = j-1; // taking left index of j
right = j+1; // taking right index of j
while (str[j] === str[right]) { // while right elementis same as center element
pal = pal + str[right]; // then add right to pal
right++; // increase right by one
}
while (left >= 0 && right < str.length) { // while left and right indices exist
if(str[left] === str[right]) { // if left value is equal to right value
pal = str[left] + pal + str[right]; // add in front and back of pal
} else { // if left value is NOT equal to right value
break; // break out of while
}
left--; // move left index by one
right++; // move right index by one
}
if(pal.length > lpal.length) { // if this pal longer than longest pal
lpal = pal; // set longest pal to be this pal
}
}
return lpal; // return longest pal
}
const longestPalindrome = str => {
let isPalindrome = false;
let test;
for (let i = str.length; i > 0; i--) {
for (let j = 0; j <= str.length - i; j++) {
isPalindrome = true;
test = str.slice(j, j+i);
for (let k = 0; k < Math.floor(test.length/2); k++) {
if (test[k] !== test[test.length-1-k]) {
isPalindrome = false;
break;
}
}
if (isPalindrome) {
return test;
}
}
}
return "";
}
const longestPalindrome1 = str => {
let longestPalindrome = "";
for (let i = 0; i < str.length; i++) {
for (let j = str.length ; j > i; j--) {
const test = str.slice(i,j);
let isPalindrome = true;
for (let k = 0; k < test.length/2; k++) {
if (test[k] !== test[test.length - 1 - k]) {
isPalindrome = false;
}
}
if (isPalindrome && test.length > longestPalindrome.length) {
longestPalindrome = test;
break;
}
}
}
return longestPalindrome;
}
const longestPalindrome2 = str => {
if (str.length > 1){
let [palindrome1, palindrome2] = [str, str];
for (let i=0;i<Math.floor(str.length/2);i++) {
if(str[i]!==str[str.length-i-1]) {
palindrome1 = longestPalindrome(str.slice(0, str.length-1));
palindrome2 = longestPalindrome(str.slice(1, str.length));
break;
}
}
return palindrome2.length > palindrome1.length ? palindrome2 : palindrome1;
} else {
return str;
}
}
console.log(longestPalindrome2("babababababababababababa"));
console.log(longestPalindrome1("babababababababababababa"));
console.log(longestPalindrome("babababababababababababa"));
console.log(lpal("babababababababababababa"));
bababababababababababab
bababababababababababab
bababababababababababab
bababababababababababab
在Leetcode上尝试以下解决方案:https://leetcode.com/problems/longest-palindromic-substring