最长回文子串-LintCode
来源:互联网 发布:手机淘宝如何清除收藏 编辑:程序博客网 时间:2024/06/06 20:34
给出一个字符串(假设长度最长为1000),求出它的最长回文子串,你可以假定只有一个满足条件的最长回文串。
您在真实的面试中是否遇到过这个题? Yes
样例:
给出字符串 “abcdzdcab”,它的最长回文子串为 “cdzdc”。
挑战 :
O(
#ifndef C200_H#define C200_H#include<iostream>#include<string>#include<vector>using namespace std;class Solution {public: /* * @param s: input string * @return: the longest palindromic substring */ string longestPalindrome(string s) { // write your code here int len = s.size(); int start=0; int maxLength = 1; if (len <= 1) return s; vector<vector<bool>> dp(len, vector<bool>(len,false)); for (int i = 0; i < len; ++i) dp[i][i] = true; for (int i = 0; i < len - 1; ++i) { if (s[i] == s[i + 1]) { dp[i][i + 1] = true; start = i; maxLength = 2; } } for (int k = 3; k <= len; ++k) { for (int i = 0; i < len - k + 1; ++i) { int j = k + i - 1; if (s[i] == s[j] && dp[i + 1][j - 1]) { dp[i][j] = true; start = i; maxLength = k; } } } return s.substr(start, maxLength); }};#endif
阅读全文
0 0
- LintCode-最长回文子串
- LintCode:最长回文子串
- lintcode,最长回文子串
- [lintcode]最长回文子串
- lintcode -- 最长回文子串
- 最长回文子串-LintCode
- LintCode 最长回文子串
- lintcode最长回文子串
- lintcode-最长回文子串-200
- LintCode之最长回文子串
- LintCode 最长回文串
- lintcode:最长回文串
- 最长回文串-LintCode
- **[Lintcode]Longest Palindromic Substring最长回文子串
- LintCode 最长回文子串(Manacher算法)
- lintcode(627)最长回文串
- LintCode之最长回文串
- 最长回文子串
- 嵌入式启动顺序
- 【设计模式 一】概述
- ubuntu安装audio recorder
- 中钰睿泓分享:设计师如何打造综合能力
- Android系统调试-程序崩溃调试
- 最长回文子串-LintCode
- squirrel-foundation状态机的使用细节
- 指针的用途
- linux grep 正则表达式
- OS X系统 手势终极设置方法+BetterTouchTool详细设置中文翻译
- genymotion模拟器安装apk的时候出现错误
- git的一些命令
- 解决Ubuntu python 使用turtle显示没有Tkinter模块问题
- 关于区块链,程序员需要了解什么