LeetCode-5*(回文)
来源:互联网 发布:秦始皇陵知乎 编辑:程序博客网 时间:2024/05/16 11:13
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"
Input: "cbbd"
Example:
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
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.
0 0
- LeetCode-5*(回文)
- leetcode 5 最长回文串
- leetcode--5--最长回文串
- leetcode回文
- 【LeetCode】回文
- LeetCode 5 最大回文子串
- LeetCode 5 : Longest Palindromic Substring ---- 最长回文
- leetcode 5-最长回文子字符串
- leetcode Problem 5 最长子回文串
- leetcode 回文系列 Palindrome
- Leetcode全回文问题
- Leetcode中回文题
- 【回文】leetcode - Shortest Palindrome
- leetcode 最长回文串
- Leetcode 336 回文对
- LeetCode------------palindrome-partitioning(回文)
- leetcode 回文问题小结
- LeetCode-10(回文检测)
- 自顶向下分析一个简单的语音识别系统(一)
- 刷机失败,估计要找售后了....
- 维护前辈代码,痛并快乐着...
- bootstrap列表
- Python机器学习实践例子——Titanic乘客生存预测模型分析
- LeetCode-5*(回文)
- 九宫格
- 461. Hamming Distance
- Git 工作区、暂存区和版本库
- bootstrap代码
- c程序设计语言之注释转换-状态机
- 小米5无法调试提示Installation failed with message Failed to establish session
- Bootstrap表格样式
- 生活感悟