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
难度:中等 来源:5. 最长回文子串
给你一个字符串 s,找到 s 中最长的回文子串。啥是回文串?就是字符可以看成是对称的,从左往右读和从右往左读是一样意思,比如:上海自来水来自海上。来看下下面的示例:
示例 1:
输入:s = "babad" 输出:"bab" 解释:"aba" 同样是符合题意的答案。
示例 2:
输入:s = "cbbd" 输出:"bb"
示例 3:
输入:s = "a" 输出:"a"
示例 4:
输入:s = "ac" 输出:"a"
提示:
1 <= s.length <= 1000 s 仅由数字和英文字母(大写和/或小写)组成
题解一:暴力破解
思路:暴力破解的思路没啥好说的,就是通过双循环来将字符串拆分成大于 2 个字符的子串,然后判断每个子串是否是回文串,保留最长回文串的长度和起始位置即可得出最长回文子串。
var longestPalindrome = function(s) { if (s.length < 2) return s let max = 1, index = 0, len = s.length for (let i = 0, l = len - 1; i < l; i++) { for (let j = i + 1; j < len; j++) { if (j - i + 1 > max && isPalindrome(s, i, j)) { max = j - i + 1 index = i } } } return s.substring(index, max + index) }; function isPalindrome(str, begin, end) { while (begin < end) { if (str[begin] !== str[end]) { return false } begin++ end-- } return true }
题解二:双指针
思路:
var longestPalindrome = function(s) { if (s == '') return '' const len = s.length let index = 0, maxL = 0, begin = 0 while (index < len) { let right = index, left = index; while (index < len && s[index + 1] == s[index]) { index++ right++ } while (right < len && left >= 0 && s[right] == s[left]) { right++ left-- } right-- left++ if (right - left + 1 > maxL) { maxL = right - left + 1 begin = left } index++ } return s.substr(begin, maxL) }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
最长回文子串
难度:中等
来源:5. 最长回文子串
给你一个字符串 s,找到 s 中最长的回文子串。啥是回文串?就是字符可以看成是对称的,从左往右读和从右往左读是一样意思,比如:上海自来水来自海上。来看下下面的示例:
示例 1:
示例 2:
示例 3:
示例 4:
提示:
题解一:暴力破解
思路:暴力破解的思路没啥好说的,就是通过双循环来将字符串拆分成大于 2 个字符的子串,然后判断每个子串是否是回文串,保留最长回文串的长度和起始位置即可得出最长回文子串。
题解二:双指针
思路:
The text was updated successfully, but these errors were encountered: