5. Longest Palindromic Substring LeetCode题解
2017-05-09 11:10
651 查看
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
题意:
给定一个字符串s,找出其中最长的回文子串,假定字符串最长为1000
Example:
Example:
Subscribe to see which companies asked this question.
题解:
详见:http://blog.csdn.net/baidu_23318869/article/details/48154899
Code【Java】
Code【C++】
题意:
给定一个字符串s,找出其中最长的回文子串,假定字符串最长为1000
Example:
Input: "babad" Output: "bab" Note: "aba" is also a valid answer.
Example:
Input: "cbbd" Output: "bb"
Subscribe to see which companies asked this question.
题解:
详见:http://blog.csdn.net/baidu_23318869/article/details/48154899
Code【Java】
public class Solution { // 在原字符串中插入指定字符 public String encode(String s, String ch) { StringBuilder ss = new StringBuilder(); for (int i = 0; i < s.length(); ++i) { ss.append(ch); ss.append(s.charAt(i)); } ss.append(ch); return ss.toString(); } // 删除原字符串中指定字符 public String decode(String s, String ch) { return s.replaceAll( ch, ""); } // 返回以当前pos为中心回文长度的一半(不含pos) int getHalfPalindromeLen(String s, int pos) { int i = 1; for ( ; 0 <= pos - i && pos + i < s.length(); ++i) { if (s.charAt(pos - i) != s.charAt(pos + i)) { break; } } return i - 1; } public String longestPalindrome(String s) { // 初始化字符串以及记录回文长度的数组 s = encode(s, "#"); int[] halfLen = new int[s.length()]; // 上一次最长回文串位置 int lastPos = 0; int lastLen = 0; for (int i = 0; i < s.length(); ++i) { if (i <= lastPos + lastLen) { int dis = i - lastPos; int cor = lastPos - dis; halfLen[i] = (cor - halfLen[cor] <= lastPos - lastLen) ? getHalfPalindromeLen(s, i) : halfLen[cor]; } else { halfLen[i] = getHalfPalindromeLen(s, i); } if (lastPos + lastLen < i + halfLen[i]) { lastPos = i; lastLen = halfLen[i]; } } // 寻找最长回文串并返回 int maxPos = 0; for (int i = 1; i < halfLen.length; ++i) { if (halfLen[maxPos] < halfLen[i]) { maxPos = i; } } return decode(s.substring(maxPos - halfLen[maxPos], maxPos + halfLen[maxPos] + 1), "#"); } }
Code【C++】
class Solution { // 在原字符串中插入指定字符 string encode(string s, char ch = '#') { stringstream ss; for (int i = 0; i < s.size(); ++i) { ss << ch << s[i]; } ss << ch; return ss.str(); } // 删除原字符串中指定字符 string decode(string s, char ch = '#') { stringstream ss; for (int i = 0; i < s.size(); ++i) { if (s[i] != ch) { ss << s[i]; } } return ss.str(); } // 返回以当前pos为中心回文长度的一半(不含pos) int getHalfPalindromeLen(string s, int pos) { int i = 1; for ( ; 0 <= pos - i && pos + i < s.size(); ++i) { if (s[pos - i] != s[pos + i]) { break; } } return i - 1; } public: string longestPalindrome(string s) { // 初始化字符串 以及记录最长回文长度数组 s = encode(s); vector<int> halfLen(s.size(), 0); // 开始计算最长回文 int lastPos = 0; int lastLen = 0; for (int i = 0; i < s.size(); ++i) { if (i <= lastPos + lastLen) { int dis = i - lastPos; int cor = lastPos - dis; halfLen[i] = (cor - halfLen[cor] <= lastPos - lastLen) ? getHalfPalindromeLen(s, i) : halfLen[cor]; } else { halfLen[i] = getHalfPalindromeLen(s, i); } // 更新lastPos以及lastLen if (lastPos + lastLen < i + halfLen[i]) { lastPos = i; lastLen = halfLen[i]; } } // 寻找最长回文位置 int maxPos = 0; for (int i = 1; i < s.size(); ++i) { if (halfLen[maxPos] < halfLen[i]) { maxPos = i; } } return decode(s.substr(maxPos - halfLen[maxPos], halfLen[maxPos] * 2 + 1)); } };
相关文章推荐
- leetcode题解-647. Palindromic Substrings && 5. Longest Palindromic Substring
- LeetCode [Day 5] Longest Palindromic Substring 题解
- LeetCode题解:Longest Palindromic Substring
- [LeetCode]题解(python):005-Longest Palindromic Substring
- Leetcode题解-5. Longest Palindromic Substring
- LeetCode - Longest Palindromic Substring 题解
- LeetCode题解 #5 Longest Palindromic Substring
- LeetCode题解-5-Longest Palindromic Substring
- LeetCode(3)题解: Longest Palindromic Substring
- LeetCode题解(Golang实现)--Longest Palindromic Substring
- LeetCode题解——Longest Palindromic Substring
- LeetCode题解:Longest Palindromic Substring
- LeetCode题解——Longest Palindromic Substring
- leetcode 5 Longest Palindromic Substring--最长回文字符串
- 【Leetcode】【python】Longest Palindromic Substring
- 【LeetCode】Longest Palindromic Substring
- leetcode——5——Longest Palindromic Substring
- LeetCode-Longest Palindromic Substring
- [LeetCode] Longest Palindromic Substring
- LeetCode -- Longest Palindromic Substring