Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

最长回文子串 #11

Open
Wild-bit opened this issue Jul 24, 2022 · 0 comments
Open

最长回文子串 #11

Wild-bit opened this issue Jul 24, 2022 · 0 comments

Comments

@Wild-bit
Copy link
Owner

Wild-bit commented Jul 24, 2022

先上代码

/**
 * @param {string} s
 * @return {string}
 */
const longestPalindrome = function (s) {
  let res = ""
  let len = s.length
  if (len < 2) return s
  let maxLen = 1
  let begin = 0
  // 创建二维数组
  let dp = Array.from(new Array(len), () => new Array(len).fill(false))
  // 先将对角线的值初始化填为true
  for (let i = 0; i < len; i++) {
    dp[i][i] = true
  }
  // 状态 dp[i][j] 表示子串s[i...j]是否是回文子串
  // 先计算左下方的值
  for (let j = 1; j < len; j++) {
    for (let i = 0; i < j; i++) {
      if (s[i] !== s[j]) {
        dp[i][j] = false
      } else if (j - i < 3) {
        // 头尾去掉没有字符和剩下一个字符的时候 一定是回文子串
        dp[i][j] = true
      } else {
        // 如果都不是,则需要看左下角的状态
        dp[i][j] = dp[i + 1][j - 1]
      }
      if (dp[i][j] && j - i + 1 > maxLen) {
        maxLen = j - i + 1
        begin = i
      }
    }
  }
  return s.substring(begin, begin + maxLen)
}
console.log(longestPalindrome("babad"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant