We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。 示例 1:
输入: "babad" 输出: "bab" 注意: "aba" 也是一个有效答案。 示例 2:
输入: "cbbd" 输出: "bb"
The text was updated successfully, but these errors were encountered:
var longestPalindrome = function(s) { // dp 解 if (!s || !s.length) return ''; let ret = s[0]; const dp = []; // 二维数组 for (let i = s.length; i >= 0; i--) { // 倒叙是因为 dp[i] 依赖于 dp[i + 1] dp[i] = []; for (let j = i; j < s.length; j++) { // 特殊情况1:相等时为同一个字符 if (j === i) { dp[i][j] = true; } else if (j - i === 1 && s[j] === s[i]) { // 特殊情况2:只有两个的时候,是否相等 dp[i][j] = true; } else if (s[i] === s[j] && dp[i + 1][j - 1]) { dp[i][j] = true; } // 当前为真,且长度大于已得到的长度 更新 ret if (dp[i][j] && j - i + 1 > ret.length) { ret = s.slice(i, j + 1); } } } return ret; }
Sorry, something went wrong.
/**
*/
function longestPalindrome(s) { if (s.length < 2) { return s; } let start = 0; let maxLength = 1; function expandAroundCenter(left, right) { while (left >= 0 && right < s.length && s[left] === s[right]) { if (right - left + 1 > maxLength) { maxLength = right - left + 1; start = left; } left--; right++; } } for (let i = 0; i < s.length; i++) { expandAroundCenter(i - 1, i + 1); expandAroundCenter(i, i + 1); } return s.substring(start, start + maxLength); }
No branches or pull requests
给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。
示例 1:
输入: "babad"
输出: "bab"
注意: "aba" 也是一个有效答案。
示例 2:
输入: "cbbd"
输出: "bb"
The text was updated successfully, but these errors were encountered: