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
20170716
https://leetcode.com/problems/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:
Input: "babad" Output: "bab"
Note: "aba" is also a valid answer. Example:
Input: "cbbd" Output: "bb"
有O(n)的算法,以后参考:http://articles.leetcode.com/longest-palindromic-substring-part-ii/ 目前看了提示后,采用下面的思路:
2n -1
class Solution(object): def longestPalindrome(self, s): """ :type s: str :rtype: str """ longest_str = "" len_s = len(s) for index in range(len_s): # 以index为中心的最长回文 l = r = index while (l >= 0) and (r < len_s): if s[l] == s[r]: l = l - 1 r = r + 1 continue else: break s1 = s[l+1:r] if len(s1) > len(longest_str): longest_str = s1 # 以index+0.5为中心的最长回文 l = index r = index + 1 while (l >= 0) and (r < len(s)): if s[l] == s[r]: l = l - 1 r = r + 1 continue else: break if l + 1 < r and r <= len(s): s2 = s[l+1:r] if len(s2) > len(longest_str): longest_str = s2 return longest_str print Solution().longestPalindrome('abbc')
The text was updated successfully, but these errors were encountered:
No branches or pull requests
问题
https://leetcode.com/problems/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:
Note: "aba" is also a valid answer.
Example:
思路
有O(n)的算法,以后参考:http://articles.leetcode.com/longest-palindromic-substring-part-ii/
目前看了提示后,采用下面的思路:
2n -1
个,n为字符串的长度解答
The text was updated successfully, but these errors were encountered: