LeetCode(5)Longest Palindromic Substring
题目
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.
分析
求给定字符串的最长回文子串。
这道题有下面三种解决思路:
暴力法,二层遍历,判断【i,j】子串是否回文,且同时记录最长长度; 显然的,暴力解决必然TLE;
字符串s中的最长回文串便是s的倒转_s与s的最长公共子串。题目转换为求最长公共子串;
动态规划解决,类似于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的更多相关文章
- leetcode 第五题 Longest Palindromic Substring (java)
Longest Palindromic Substring Given a string S, find the longest palindromic substring in S. You may ...
- leetcode第五题--Longest Palindromic Substring
Problem:Given a string S, find the longest palindromic substring in S. You may assume that the maxim ...
- Leetcode:【DP】Longest Palindromic Substring 解题报告
Longest Palindromic Substring -- HARD 级别 Question SolutionGiven a string S, find the longest palindr ...
- leetcode--5 Longest Palindromic Substring
1. 题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximu ...
- LeetCode(3)Longest Substring Without Repeating Characters
题目: Given a string, find the length of the longest substring without repeating characters. For examp ...
- LeetCode (32) Longest Valid Parentheses
题目 Given a string containing just the characters '(' and ')', find the length of the longest valid ( ...
- 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 ...
- LeetCode(128) Longest Consecutive Sequence
题目 Given an unsorted array of integers, find the length of the longest consecutive elements sequence ...
- LeetCode(14)Longest Common Prefix
题目 Write a function to find the longest common prefix string amongst an array of strings. 分析 该题目是求一个 ...
随机推荐
- dubbo服务降级(1)
1. 在 dubbo 管理控制台配置服务降级 上图的配置含义是:consumer 调用 com.zhang.HelloService 的方法时,直接返回 null,不发起远程调用. 实际操作是:在 z ...
- spring4、hibernate4整合xml配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- Servlet--HttpServlet
一.Servlet 接口(javax.servlet) 定义: public interface Servlet Implemented by: FacesServlet, Gene ...
- CSS font-family 字体名称一览表
windows常见内置中文字体 字体中文名 字体英文名 宋体 SimSun(浏览器默认) 黑体 SimHei 微软雅 ...
- Yii2 的快速配置 api 服务 yii2-fast-api
yii2-fast-api yii2-fast-api是一个Yii2框架的扩展,用于配置完善Yii2,以实现api的快速开发. 此扩展默认的场景是APP的后端接口开发,因此偏向于实用主义,并未完全采用 ...
- Jenkins中启动从节点时,出现问题如何解决,问题:No Known Hosts...
Jenkins中,启动从节点时,出现如下问题如何解决:/root/.ssh/known_hosts [SSH] No Known Hosts file was found at /root/.ssh/ ...
- CentOS-语言设置
查看所有的locale语言 # locale -a # locale -a|grep en 查看当前操作系统使用的语言 # echo $LANG 设置系统locale语言为中文环境(永久生效) # v ...
- HTML之元素分类
一.元素展示类型 在HTML本身定义了很多元素,这些元素在网页上展示的时候都会有自己的默认状态,例如有些元素在默认状态下对高宽的属性设置不起作用,有些元素都默认情况下都独立一行显示,这种现象我们称之为 ...
- stixel world论文总结
1.The Stixel World - A Compact Medium Level Representation of the 3D-World:http://pdfs.semanticschol ...
- pbr 5.2.1需使用中科大的源,豆瓣的不行
-bash-4.2$ .tox/tempest/bin/pip install pbr==5.2.1DEPRECATION: Python 2.7 will reach the end of its ...