题目

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. Java Excel API的使用

    https://wenku.baidu.com/view/724cc9e2dd88d0d232d46a1b.html

  2. Netty-promise

    public class TimeEncoder extends ChannelOutboundHandlerAdapter { @Override public void write(Channel ...

  3. NETCORE MVC模块化

    NETCORE MVC模块化 ASP.NETCORE MVC模块化编程 前言 记得上一篇博客中跟大家分享的是基于ASP.NETMVC5,实际也就是基于NETFRAMEWORK平台实现的这么一个轻量级插 ...

  4. CSS——制作天天生鲜主页

    终于做好了! index.html: <!DOCTYPE html> <html lang="en"> <head> <meta char ...

  5. 项目协作管理平台-teambition和tapd--深度体验

    ​ 一.分析目的 通过分析2B产品中的团队协作管理软件的对比分析,用于为公司团队协作软件的选型做产考. 二.竞品归属市场概况 2.1.目标用户群及需求 主要面向企业用户,用于解决企业不同地域以及不同职 ...

  6. 小G搭积木

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

  7. P4869 罪犯分组

    思路: 明显的dp,虽然我想到了二进制模拟,想到了转移,但还是先看了题解,原来真是这样,,,,不是第三题吗? 用f[i]表示,对于前i个罪犯最少需要分几组. 对于每个状态用二进制表示,第i位上1,0表 ...

  8. LaTeX小技巧——File ended while scanning use of \@writefile错误的

    早上在修改编译论文时发现了这个问题,仔细检查代码并没发现错误,一时也找不到具体的解决办法.我一直以为是因为runaway argument的错误提示,可实际上就是因为aux文件没有完整输入,导致上次编 ...

  9. JMeter配置元件作用域

  10. checkbox 全选 单选的使用

    绑定数据 if (!IsPostBack) { using (UsersDataContext con = new UsersDataContext()) { Repeater1.DataSource ...