题目

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. 【转载】.NET模拟POST登录并保持登录状态

    好了,还是由于工作需要 要登录一个网站并且模拟点击下载某些东西 原理就是先对一个地址(地址是用户名和密码输入框所在的form的action对应的页面)进行POST提交用户名和密码(不考虑验证码,当然验 ...

  2. SQLServer 数据库查看死锁、堵塞的SQL语句

    http://www.cnblogs.com/zhuque/archive/2012/11/08/2763343.html 死锁和堵塞一直是性能测试执行中关注的重点. 下面是我整理的监控sql ser ...

  3. Arrays数组的常用方法

    下面代码主要说明了Arrays数组的几个常用方法(红色字体) import java.util.Scanner;import java.util.Arrays;      public class T ...

  4. cuda-convnet windows8下编译

    编译环境: windows8.1 Anaconda python2.7 Visual studio 2012 CUDA6.0 Pthread for windows Intel Math Kernel ...

  5. Java基础和JDK5.0新特性

    Java基础 JDK5.0新特性 PS: JDK:Java Development KitsJRE: Java Runtime EvironmentJRE = JVM + ClassLibary JV ...

  6. jquery 操作select 资料

    每一次操作select的时候,总是要出来翻一下资料,不如自己总结一下,以后就翻这里了. 比如<select class="selector"></select&g ...

  7. 【原创】新手用外挂来学C语言,外挂入门教程【2013.03.12更新V5.1版

    目录 e@vZg8Ie  第一章 配置编译环境    - 5 - W7~_XI  1.1 安装CB和gcc    - 6 - fj( WH L  1.2 使用gcc编写我们的第一个C程序    - 1 ...

  8. DBMS_OUTPUT in SQL Developer

    I fell sad for all the programmers(including) that has to SQL Developer as sql IDE. However, if you ...

  9. handshake_failure

    在java 1.6版本中,通过HttpsURLConnection请求class 1(多发生于免费ssl证书,比如沃通或者startssl的)的https网络地址时,可能会报握手异常: Caused ...

  10. PetShop安装失败

    报错提示: Security This installer has encountered an unexpected error installing this package. This may ...