题目

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的更多相关文章

  1. 【LeetCode每天一题】Longest Palindromic Substring(最长回文字串)

    Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...

  2. LeetCode第五题:Longest Palindromic Substring

    Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...

  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(5)Longest Palindromic Substring

    题目 Given a string S, find the longest palindromic substring in S. You may assume that the maximum le ...

  6. 刷题5. Longest Palindromic Substring

    一.题目说明 Longest Palindromic Substring,求字符串中的最长的回文. Difficuty是Medium 二.我的实现 经过前面4个题目,我对边界考虑越来越"完善 ...

  7. 【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 ...

  8. LeetCode第[5]题(Java):Longest Palindromic Substring 标签:String、动态规划

    题目中文:求最长回文子串 题目难度:Medium 题目内容: Given a string s, find the longest palindromic substring in s. You ma ...

  9. leetcode 第五题 Longest Palindromic Substring (java)

    Longest Palindromic Substring Given a string S, find the longest palindromic substring in S. You may ...

随机推荐

  1. LeetCode() Super Ugly Number

    用了优先队列,还是超时 class Solution { public: int nthSuperUglyNumber(int n, vector<int>& primes) { ...

  2. jsonp案例

    <button id="btn">click</button><script type="text/javascript"> ...

  3. js内存泄露的几种情况

    想解决内存泄露问题,必须知道什么是内存泄露,什么情况下出现内存泄露,才能在遇到问题时,逐个排除.这里只讨论那些不经意间的内存泄露. 一.什么是内存泄露 内存泄露是指一块被分配的内存既不能使用,又不能回 ...

  4. unsigned char 类型

    在蓝牙4.0的开发中,很多数据类型都用到了 unsigned char ,我觉得用这个类型的一个原因是相比较于整型,它占的空间更少. 比如: unsigned char a = 1;  // 占1个字 ...

  5. less和sass

    sass 总体来说应用是和less差不多的,但是也有所不同     sass是用“$”符号来命名 然后加值来先引入后使用的方式.     同时也应该注意到的是sass有两种后缀名文件:一种后缀名为sa ...

  6. zend studio 配置 apache服务器事宜

    安装好 zend studio后,配置 apache服务器时,设置 configuration directory时,需选中 xampp\apache里面的 conf 文件夹,即完整的路径为: *\x ...

  7. caffe初步实践---------使用训练好的模型完成语义分割任务

    caffe刚刚安装配置结束,乘热打铁! (一)环境准备 前面我有两篇文章写到caffe的搭建,第一篇cpu only ,第二篇是在服务器上搭建的,其中第二篇因为硬件环境更佳我们的步骤稍显复杂.其实,第 ...

  8. linux 1-100的累加

    [   ]   判断式.它的使用和test命令一样 [ ]的判断符,只会返回2种值.0(真) 非0(假) -gt 大于-lt 小于-eq 等于-ne 不等于-ge 大于等于-le 小于等于 while ...

  9. OpenStack部署工具总结

    目前感觉比较简单直观的部署工具有RDO.devstack.Fuel等: 1. RDO https://openstack.redhat.com/Quickstart REDHAT出品,支持Redhat ...

  10. [wxWidgets] 2. 重访“Hello World” 程序

    这是四年多来在博客园的第二篇博客.有了上一次的排版使用经验,这一篇文章应该有些进步(^_^). 闲话按下不表,言归正传.在编译.成功运行了上一个helloWorld.cpp(后文中'hw'简称hell ...