题目

Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.

分析

求给定字符串的最长回文子串。

这道题有下面三种解决思路:

  1. 暴力法,二层遍历,判断【i,j】子串是否回文,且同时记录最长长度; 显然的,暴力解决必然TLE;

  2. 字符串s中的最长回文串便是s的倒转_s与s的最长公共子串。题目转换为求最长公共子串;

  3. 动态规划解决,类似于lcs的解法,数组flag[i][j]记录s从i到j是不是回文,参考网址

    3.1. 首先初始化,i>=j时,flag[i][j]=true,这是因为s[i][i]是单字符的回文,当i>j时,为true,是因为有可能出现flag[2][1]这种情况,比如bcaa,当计算s从2到3的时候,s[2]==s[3],这时就要计算s[2+1] ?= s[3-1],总的来说,当i>j时置为true,就是为了考虑j=i+1这种情况。

    3.2. 接着比较s[i] ?= s[j],如果成立,那么flag[i][j] = flag[i+1][j-1],否则直接flag[i][j]=false;

AC代码

class Solution {
public:
string longestPalindrome(string s) {
int len = s.length(), max = 1, ss = 0, tt = 0;
bool flag[len][len];
for (int i = 0; i < len; i++)
for (int j = 0; j < len; j++)
if (i >= j)
flag[i][j] = true;
else flag[i][j] = false;
for (int j = 1; j < len; j++)
for (int i = 0; i < j; i++)
{
if (s[i] == s[j])
{
flag[i][j] = flag[i+1][j-1];
if (flag[i][j] == true && j - i + 1 > max)
{
max = j - i + 1;
ss = i;
tt = j;
}
}
else flag[i][j] = false;
}
return s.substr(ss, max);
}
};

其它解法

class Solution {
public:
/*方法一:暴力法*/
string longestPalindrome1(string s) {
if (s.empty())
return false;
//如果源串本身便是回文,则返回源串
if (isPalindrome(s))
return s; int len = s.length(), maxLen = 0;
string ret = "";
for (int i = 0; i < len; ++i)
{
for (int j = i + 1; j < len; ++j)
{
string str = s.substr(i, j - i);
if (isPalindrome(str) && (j - i) > maxLen)
{
ret = str;
maxLen = j - i;
}//if
else
continue;
}//for
}//for
return ret;
} /*方法二:字符串s中的最长回文串便是s的倒转_s与s的最长公共子串*/
string longestPalindrome2(string s) {
if (s.empty())
return false;
//如果源串本身便是回文,则返回源串
if (isPalindrome(s))
return s; //求字符串s的倒置
string rs = s;
reverse(rs.begin(), rs.end()); return lcs(s, rs);
} /*方法三:动态规划*/
string longestPalindrome(string s)
{
if (s.empty())
return false;
int len = s.length(), maxLen = 1, from = 0, to = 0;
vector<vector<int>> flag(len, vector<int>(len,0));
for (int i = 0; i < len; ++i)
{
for (int j = 0; j < len; ++j)
{
//值为1表示 i,j 范围子串为回文串,i>j时值为1,针对j==i+1的情况
if (i >= j)
flag[i][j] = 1;
}//for
}//for for (int j = 1; j < len; ++j)
{
for (int i = 0; i < j; ++i)
{
/*判断从i到j的子串是否为回文串,若字符相等*/
if (s[i] == s[j])
{
/*则i到j是否为子串由 i+1 到 j-1 决定*/
flag[i][j] = flag[i + 1][j - 1]; /*更新最长子串长度和起始结束位置*/
if (flag[i][j] == 1 && (j - i + 1) > maxLen)
{
maxLen = j - i + 1;
from = i;
to = j;
}//if
}else
flag[i][j] = 0;
}//for
}//for
return s.substr(from, maxLen);
} /*求s和rs的最长公共子串*/
string lcs(string s, string rs)
{
return "";
}
/*判断字符串s是否为回文串*/
bool isPalindrome(string s)
{
if (s.empty())
return false;
int lhs = 0, rhs = s.size() - 1;
while (lhs < rhs)
{
if (s[lhs] != s[rhs])
return false;
++lhs;
--rhs;
}//while
return true;
}
};

LeetCode(5)Longest Palindromic Substring的更多相关文章

  1. leetcode 第五题 Longest Palindromic Substring (java)

    Longest Palindromic Substring Given a string S, find the longest palindromic substring in S. You may ...

  2. leetcode第五题--Longest Palindromic Substring

    Problem:Given a string S, find the longest palindromic substring in S. You may assume that the maxim ...

  3. Leetcode:【DP】Longest Palindromic Substring 解题报告

    Longest Palindromic Substring -- HARD 级别 Question SolutionGiven a string S, find the longest palindr ...

  4. leetcode--5 Longest Palindromic Substring

    1. 题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximu ...

  5. LeetCode(3)Longest Substring Without Repeating Characters

    题目: Given a string, find the length of the longest substring without repeating characters. For examp ...

  6. LeetCode (32) Longest Valid Parentheses

    题目 Given a string containing just the characters '(' and ')', find the length of the longest valid ( ...

  7. LeetCode(76) Minimum Window Substring

    题目 Given a string S and a string T, find the minimum window in S which will contain all the characte ...

  8. LeetCode(128) Longest Consecutive Sequence

    题目 Given an unsorted array of integers, find the length of the longest consecutive elements sequence ...

  9. LeetCode(14)Longest Common Prefix

    题目 Write a function to find the longest common prefix string amongst an array of strings. 分析 该题目是求一个 ...

随机推荐

  1. ExceptionHandlerMiddleware中间件如何呈现“定制化错误页面”

    ExceptionHandlerMiddleware中间件如何呈现“定制化错误页面” DeveloperExceptionPageMiddleware中间件利用呈现出来的错误页面实现抛出异常和当前请求 ...

  2. PS高级特训班 百度云资源(价值2180元)

    课程目录:   第1章第一期1第一节 火焰拳头1:12:252第二节 荷叶合成00:05:143第三节 新年巨惠海报(一)1:00:374第四节 新年巨惠海报(二)1:05:345第五节 美食印刷品1 ...

  3. 小G搭积木

    A小 G 搭积木文件名 输入文件 输出文件 时间限制 空间限制box.cpp box.in box.out 2s 128MB题目描述小 G 喜欢搭积木.小 G 一共有 n 块积木,并且积木只能竖着一块 ...

  4. ECharts3.0介绍、入门

    ECharts 特性介绍 ECharts,一个纯 Javascript 的图表库,可以流畅的运行在 PC 和移动设备上,兼容当前绝大部分浏览器(IE8/9/10/11,Chrome,Firefox,S ...

  5. JS类对象实现继续的几种方式

    0. ES6可以直接使用class,extends来继承. 1.  原型继承 父类: function Persion(name,age){ this.name = name; this.age = ...

  6. C# 對 List<string> 取交集、補集、超集、串聯

    List<string> ls1 =new List<string> { "a", "b", "c", " ...

  7. 8.对于.NET的初步理解和介绍

    好久没写博客了,最近心情比较low,不知道为什么.很流行的一个问题叫做:如果你明天就挂了,那么你最后悔的事情将会是什么.我想了两个月,答案是不知道,无所谓.这样不好,那这个问题先放一边吧,我们开始这一 ...

  8. HBase数据模型(1)

    HBase数据模型(1) HBase数据模型(2) 1.0 HBase的特性 Table HBase以表(Table)的方式组织数据,数据存储在表中. Row/Column 行(Row)和列(Colu ...

  9. 设置DIV随滚动条滚动而滚动

    有段时间没有碰Web端了,最近做了个功能,需要做个DIV随滚动条滚动而滚动,mark一下: 源码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1 ...

  10. Windows Azure 配置Active Directory 主机(3)

    步骤 4:在 CloudSite 中安装附加域控制器 1.登录到 YourVMachine,单击“开始”,键入“dcpromo”,然后按 Enter. 2.在“欢迎使用”页上,单击“下一步”. 3.在 ...