LeetCode-5*(回文)
2017-04-03 10:02
295 查看
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"
Subscribe to see which companies asked this question.
Example:
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Example:
Input: "cbbd"
Output: "bb"
class Solution { public: string longestPalindrome(string s) { if(s.empty()) return ""; //空串 if(s.size() == 1) return s; //单字符串 int start = 0, len = 1; //回文子串开始,长度 for(int i = 0;i<s.size();){ if(s.size()-i <= len/2) break; int j = i, k = i; while(k<s.size()-1 && s[k] == s[k+1]) k++; //掠过重复字符 i = k+1; //i存储回文子串对称轴心,如有重复字符的话,指向重复字符的后一个
while(k<s.size()-1 && j>0 && s[k+1]==s[j-1]) { k++; j--; } int newlen=k-j+1; if(newlen>len){ len = newlen; start = j; } } return s.substr(start,len); } };
Subscribe to see which companies asked this question.
相关文章推荐
- LeetCode 234 Palindrome Linked List(回文链表)(*)(?)
- leetcode 234. Palindrome Linked List 回文链表的判断 + 双指针
- (LeetCode)Valid Palindrome --- 判断回文字符串
- Leetcode479. 找到两个位数相同的数相乘的最大回文积
- [LeetCode系列] 最长回文子串问题
- leetcode5-Longest Palindromic Substring(最长回文子串)
- LeetCode 9. 回文数
- leetcode最长回文字符串_动态规划
- leetcode 5. Longest Palindromic Substring(回文和Manacher算法)
- 【leetcode】第9题:判断整型数是否为回文数
- leetcode 回文问题小结
- [Leetcode] 回文问题
- leetcode题目 寻找最长回文字串
- LeetCode 266. Palindrome Permutation (回文排列)$
- [LeetCode] Palindrome Number 验证回文数字
- 【LeetCode-面试算法经典-Java实现】【05-Longest Palindromic Substring(最大回文字符串)】
- LeetCodeOJ_5_m_Longest Palindromic Substring(最长回文子串)
- LeetCode 5. Longest Palindromic Substring(最长回文连续子串)
- LeetCode-409-Longest Palindrome(求字符串可组成的最长回文长度)
- [LeetCode] Palindrome Permutation 回文全排列