5. Longest Palindromic Substring
Posted xingguozhiming
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了5. Longest Palindromic Substring相关的知识,希望对你有一定的参考价值。
5. Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example 1:
Input: "babad" Output: "bab" Note: "aba" is also a valid answer.
Example 2:
Input: "cbbd" Output: "bb"
题意:给出一个字符串,求其最长回文子串
代码如下:
var longestPalindrome = function(s) { let max=0,start=0; if(s.length<2) return s; // 从位置i开始到j结束,返回最长的回文子串长度 let hasPalindrome=function(str,i,j){ while(i>=0 && j<str.length && str.charAt(i)==str.charAt(j)){ i--; j++; } if(max<j-i-1){ start=i+1; max=j-i-1; } } // 区分奇偶回文 for(let i=0;i<s.length;i++){ hasPalindrome(s,i,i); hasPalindrome(s,i,i+1); } return s.substring(start,start+max); };
以上是关于5. Longest Palindromic Substring的主要内容,如果未能解决你的问题,请参考以下文章
#Leetcode# 5. Longest Palindromic Substring
5. Longest Palindromic Substring - Unsolved
5. Longest Palindromic Substring
5. Longest Palindromic Substring