题目
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.
答案
一道动规题目,每次用当前位置columnIndex跟前面位置rowIndex去比较时,如果相同的话,就去看columnIndex- 1和rowIndex+1字母的状态,其实在走到columnIndex时,columnIndex-1和它自己前面的字母是比较过了的,将这个状态记下来就行。
讲真,我这代码效率不高,应该还有其他思路,再想想。
代码
#define MAX_LENGTH 1001
class Solution {
public:
string longestPalindrome(string s) {
if(s.empty())
{
return string("");
}
bool charRelFlag[MAX_LENGTH][MAX_LENGTH];
int sLen = s.size();
int subStart = ;
int subEnd = ;
int rowIndex,columnIndex; for(rowIndex = ; rowIndex < sLen; ++ rowIndex)
{
charRelFlag[rowIndex][rowIndex] = true;
for(columnIndex = rowIndex + ; columnIndex < sLen; ++ columnIndex)
{
charRelFlag[rowIndex][columnIndex] = false;
}
} int maxLen = ;
for(columnIndex = ; columnIndex < sLen; ++ columnIndex)
{
for(rowIndex = ; rowIndex < columnIndex; ++ rowIndex)
{
if(s[rowIndex] == s[columnIndex])
{
if(rowIndex + == columnIndex)
{
charRelFlag[rowIndex][columnIndex] = true;
if(maxLen < )
{
maxLen = ;
subStart = rowIndex;
subEnd = columnIndex;
}
}else
{
if(charRelFlag[rowIndex + ][columnIndex - ] == true)
{
charRelFlag[rowIndex][columnIndex] = true;
if(maxLen < columnIndex - rowIndex + )
{
maxLen = columnIndex - rowIndex + ;
subStart = rowIndex;
subEnd = columnIndex;
}
}
} }
}
} return s.substr(subStart,maxLen);
}
};
leetcode-【中等题】5. Longest Palindromic Substring的更多相关文章
-
【LeetCode每天一题】Longest Palindromic Substring(最长回文字串)
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
-
LeetCode第五题:Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
-
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(5)Longest Palindromic Substring
题目 Given a string S, find the longest palindromic substring in S. You may assume that the maximum le ...
-
刷题5. Longest Palindromic Substring
一.题目说明 Longest Palindromic Substring,求字符串中的最长的回文. Difficuty是Medium 二.我的实现 经过前面4个题目,我对边界考虑越来越"完善 ...
-
【LeetCode算法题库】Day2:Median of Two Sorted Arrays &; Longest Palindromic Substring &; ZigZag Conversion
[Q4] There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of th ...
-
LeetCode第[5]题(Java):Longest Palindromic Substring 标签:String、动态规划
题目中文:求最长回文子串 题目难度:Medium 题目内容: Given a string s, find the longest palindromic substring in s. You ma ...
-
leetcode 第五题 Longest Palindromic Substring (java)
Longest Palindromic Substring Given a string S, find the longest palindromic substring in S. You may ...
随机推荐
-
CentOS7 编译安装 nginx-1.10.0
对于NGINX 支持epoll模型 epoll模型的优点 定义: epoll是Linux内核为处理大批句柄而作改进的poll,是Linux下多路复用IO接口select/poll的增强版本,它能显著的 ...
-
POJ 2653 Pick-up sticks (线段相交)
题意:给你n条线段依次放到二维平面上,问最后有哪些没与前面的线段相交,即它是顶上的线段 题解:数据弱,正向纯模拟可过 但是有一个陷阱:如果我们从后面向前枚举,找与前面哪些相交,再删除前面那些相交的线段 ...
-
iOS 收起键盘的几种方式
iOS 收起键盘的几种方式 1.一般的view上收起键盘 // 手势 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ ...
-
Python学习笔记之字典
一.创建和使用字典 1.创建字典 phonebook={'Alice':'2341','Beth':'9102','Cecil':'3258'} 2.dict,通过映射创建字典 >>> ...
-
2106 Problem F Shuffling Along 中石油-未提交-->;已提交
题目描述 Most of you have played card games (and if you haven’t, why not???) in which the deck of cards ...
-
数论 UVA 11388
这道题是关于两个数的最大公约数和最小公倍数的题目.给你两个数字g,l,分别表示最大公约数和最小公倍数.要求你找到两个数a,b,要求这两个数的最大公约数和最小公倍数为所给的两个数.如果存在多组数字符合这 ...
-
关于oralce字符集问题(复制别人的,纯属自己学习)
本文主要讨论以下几个部分: 1.如何查看查询oracle字符集. 2.修改设置字符集以及常见的oracle utf8字符集 3.oracle exp 字符集问题 正文: 一.字符集参数 影响Oracl ...
-
java中的url 编码与解码
什么是application/x-www-form-urlencoded字符串? 答:它是一种编码类型.当URL地址里包含非西欧字符的字符串时,系统会将这些字符转换成application/x-www ...
-
dapper关联关系查询小测试
测试实体类(表结构) public class User { public int user_id { get; set; } public string user_name { get; set; ...
-
ubuntu后台运行命令
参考 https://blog.csdn.net/shaozg168/article/details/6979337 nohup ./test.sh &