题目

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. D. Restructuring Company 并查集 + 维护一个区间技巧

    http://codeforces.com/contest/566/problem/D D. Restructuring Company time limit per test 2 seconds m ...

  2. Windows3

    windows安装后的配置 没有网络适配器, 将USB中的驱动精灵的安装程序安装在win上, 启动精灵, 提示无法连接到网络, 使用Android类型的手机中的QQ浏览器扫码下载 win会有一些开机自 ...

  3. 模板引擎doT.js

    作为一名前端攻城师,经常会遇到从后台ajax拉取数据再显示在页面的情境,一开始我们都是从后台拉取再用字符串拼接的方式去更达到数据显示在页面! <!-- 显示区域 --> <div i ...

  4. window.open()弹出窗口参数说明及居中设置

    window.open()可以弹出一个新的窗口,并且通过参数控制窗口的各项属性. 最基本的弹出窗口代码 window.open('httP://codeo.cn/'); window.open()各参 ...

  5. 《Ionic 2 实例开发》发布

    Ionic 2系列教程集结成册,在百度阅读上架发布,名为<Ionic 2实例开发>(点击书名将打开地址:http://yuedu.baidu.com/ebook/ba1bca51e4189 ...

  6. JFinal视频教程-JFnal学院分享课

    最近JFinal学院出了JFinal视频教程分享课,请笑纳~ 课程列表: 1.[JFinal版]微信小程序富文本渲染解决方案-html2wxml4J分享课 这个课程主要讲的是使用基于JFinal开发的 ...

  7. 【装载】删除Oracle11G

    卸载Oracle步骤:1.停止所有与ORACLE相关的服务.2. 使用OUI(Oracle Universal Installer)卸载Oracle软件.   “开始”->“程序”->“O ...

  8. windows下安装pm2

    安装pm2 npm install pm2 -g 添加系统环境变量 PM2_HOME=C:\Users\PCONE\.pm2 打开新的cmd命令行窗口,执行以下命令来安装服务 pm2-service- ...

  9. 关于纠正《Hive权威指南》中的结论~“hive在使用set自定义变量时,hivevar命名空间是可选的”~的论证

    背景: 根据<Hive权威指南>上讲,在hive-0.8.0以后可以使用--define key=value命令定义用户自定义的变量以便在Hive脚本中引用.当用户使用这个功能时,Hive ...

  10. LibreOJ #100. 矩阵乘法

    内存限制:256 MiB 时间限制:2000 ms 标准输入输出 题目类型:传统 评测方式:文本比较 上传者: 匿名     模版 以前一直不过样例原来是读入优化没写负数.. 屠龙宝刀点击就送 #in ...