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. 分析 该题目是求一个 ...
随机推荐
- linux替换文件中的某个字符串的命令sed
sed -i 's/java-7-oracle/java-8-oracle/g' /etc/init.d/tomcat7 上面的命令是将tomcat7中的java-7-oracle替换为java-8- ...
- 移动端rem单位和px单位换算
rem单位是根据html元素的单位在页面根据不同的手机屏幕分辨率动态整体的按比例缩小或放大字体. 假如html{font-size: 14px;},那么1rem=14px; 一个div宽度48px,那 ...
- 【extjs6学习笔记】Mastering Ext JS, 2nd Edition
我不知道在别人看来,我是什么样的人:但在我自己看来,我不过就象是一个在海滨玩耍的小孩,为不时发现比寻常更为光滑的一块卵石或比寻常更为美丽的一片贝壳而沾沾自喜,而对于展现在我面前的浩瀚的真理的海洋,却全 ...
- PHP的socket通信原理及实现
对TCP/IP.UDP.Socket编程这些词你不会很陌生吧?随着网络技术的发展,这些词充斥着我们的耳朵.那么我想问: 1. 什么是TCP/IP.UDP?2. Sock ...
- js当前日期
function CurentTime() { var now = new Date(); var year = now.getFullYear(); ...
- 中国区 Azure 服务和定价模式概述
由世纪互联运营的 Microsoft Azure 是第一个在中国正式商用,符合中国政府相关法规要求的国际化公有云服务.本文剖析了由世纪互联运营的 Microsoft Azure 的运营模式.采购模式. ...
- 机器学习经典算法之SVM
SVM 的英文叫 Support Vector Machine,中文名为支持向量机.它是常见的一种分类方法,在机器学习中,SVM 是有监督的学习模型. 什么是有监督的学习模型呢?它指的是我们需要事先对 ...
- python 产生随机数
Python中的random模块用于生成随机数.下面介绍一下random模块中最常用的几个函数. random.random random.random()用于生成一个0到1的随机符点数: 0 < ...
- PAT (Basic Level) Practise (中文)-1039. 到底买不买(20)
PAT (Basic Level) Practise (中文)-1039. 到底买不买(20) http://www.patest.cn/contests/pat-b-practise/1039 小红 ...
- Exchange邮箱搭建
出现的问题: System.Runtime.InteropServices.COMException(0x8004020F): The server rejected one or more reci ...