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.
答案
一道动规题目,每次用当前位置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 ...
随机推荐
- ObjectOutputStream序列化问题
ObjectOutputStream序列化对象传输时,为了节省开销,会自动比较以前序列化过的对象,如果一致(指内存,不比较内容),则自动引用以前用过的对象,这就造成了传输到对方的对象总是第一次序列化的 ...
- POJ做题笔记:1000,1004,1003
1000 A+B Problem 题目大意:输入两个数a和b,输出他们的和. 代码: #include <stdio.h> int main() { int a, b; while (sc ...
- java中的反射,知道类名创建类,还可以设置私有属性的值
刚刚学到了反射,感觉反射的功能好强大,所以想写篇博客记录下自己的学习成果. 利用反射来创建对象. Class c1=Class.forName("test.Person");//通 ...
- WIN7 WIN8 WIN10你们的WmiPrvSE.exe系统占用资源厉害吗?
大家一起来做个实验吧.把你的支付宝安全控件和支付宝数字证书两个东东都卸掉.试试看电脑是否年轻几岁? 出处:百度贴吧原帖
- JS转换HTML转义符
JS转换HTML转义符 //去掉html标签 function removeHtmlTab(tab) { return tab.replace(/<[^<>]+?>/g,'') ...
- JS Date当前时间:获取日期时间方法在各浏览器中的差异
转自:http://www.feiesoft.com/00047/<script type="text/javascript"> // JS Date当前时间获取方法在 ...
- [转]windows下编译及使用libevent
http://www.cnblogs.com/luxiaoxun/p/3603399.html Libevent官网:http://libevent.org/ windows 7下编译: 编译环境: ...
- java同一个类不同方法间的同步
对象的方法中一旦加入synchronized修饰,则任何时刻只能有一个线程访问synchronized修饰的方法.假设有个数据对象拥有写方法与读方法,多线程环境中要想保证数据的安全,需对该对象的读写方 ...
- linux下重置mysql的root密码
# /etc/init.d/mysql stop # mysqld_safe --user=mysql --skip-grant-tables --skip-networking & # my ...
- AngularJs之$scope对象(作用域)
一.作用域 AngularJs中的$scope对象是模板的域模型,也称为作用域实例.通过为其属性赋值,可以传递数据给模板渲染. 每个$scope都是Scope类的实例,Scope类有很多方法,用于 ...