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.

  分析:   DP 问题

Initial state:
table[i][i] =  true.
table[i][i+1] = (s[i]==s[i+1]);

State Change:
if s[i]==s[j], table[i][j]=table[i+1][j-1]

class Solution {
public:
string longestPalindrome(string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int n = s.size();
if(n < ) return s;
vector<vector<bool>> table(n, vector<bool>(n,false)); //init length 1
for(int i = ; i< n; i++)
table[i][i] = true; int len, maxlen = , maxStart = ,i,j;
for(len = ; len <= n ; len ++)
{
for(i = ; i< n - len + ; i++)
{
j = i + len - ; if(s[i] == s[j] &&len == )
{
table[i][j] = true;
maxlen = len;
maxStart = i;
} else if (s[i] == s[j] && table[i+][j-])
{
table[i][j] = true;
maxlen = len;
maxStart = i;
} }
} return s.substr(maxStart , maxlen);
}
};

这里解释下为什么len ==2 要单独处理: 因为table[i][j]只有上三角的值有意义,即 j >= i ; 当len = 2 时,table[i+1][j-1] j-1= i+2-1-1 = i  即此时j-1< i+1 ; 所以要单独处理

reference :http://leetcode.com/2011/11/longest-palindromic-substring-part-i.html

http://www.geeksforgeeks.org/dynamic-programming-set-12-longest-palindromic-subsequence/

LeetCode_Longest Palindromic Substring的更多相关文章

  1. 最长回文子串-LeetCode 5 Longest Palindromic Substring

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

  2. leetcode--5. Longest Palindromic Substring

    题目来自 https://leetcode.com/problems/longest-palindromic-substring/ 题目:Given a string S, find the long ...

  3. [LeetCode] Longest Palindromic Substring 最长回文串

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

  4. No.005:Longest Palindromic Substring

    问题: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...

  5. Leetcode Longest Palindromic Substring

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

  6. 【leedcode】 Longest Palindromic Substring

    Given a , and there exists one unique longest palindromic substring. https://leetcode.com/problems/l ...

  7. [LeetCode_5] Longest Palindromic Substring

    LeetCode: 5. Longest Palindromic Substring class Solution { public: //动态规划算法 string longestPalindrom ...

  8. 5. Longest Palindromic Substring

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

  9. leetcode-【中等题】5. Longest Palindromic Substring

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

随机推荐

  1. GET: https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login? loginicon=true &uuid=odcptUu2JA==&tip=0

    GET: https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login? loginicon=true &uuid=odcptUu2JA==&am ...

  2. 【转】Ubuntu10.04上编译Android源码(Build Android source in Ubuntu10.04 Platform)

    原文网址:http://blog.csdn.net/chenyafei617/article/details/6570928 一.Introduction 今天我们就来谈谈如何在Ubuntu平台上面编 ...

  3. 笔试、面试重点总结:WIN32、MFC与Linux

    win32 1. Win32应用程序的基本类型. 2. 创建win32窗口程序的几个步骤,及使用到的函数. 3. nmake 与 makefile. 4. 有哪些字符集? Win32对于各种字符集如何 ...

  4. 飘逸的python - 解决一个有限制的组合需求

    假设有一个团队技能的需求. 这类技能是要集齐所有指定的人就能激活. 但是因为同一个人又2种身份存在,比如杨戬/神杨戬,于是便产生了组合. 这种组合跟普通组合不一样,普通组合可以随意组合.而这种组合是每 ...

  5. [Hapi.js] Serving static files

    hapi does not support serving static files out of the box. Instead it relies on a module called Iner ...

  6. Android应用程序绑定服务(bindService)的过程源代码分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6745181 Android应用程序组件Serv ...

  7. [IDEA学习笔记][keymap]

    一个总站: http://www.youmeek.com/ 常用的快捷键keymap 提示: ctrl+N:快速打开一个类 Ctrl+P 方法参数提示显示 Ctrl+J 提示自定义模板 Ctrl+O ...

  8. 伪元素first-letter(首字母变大)

    让首字母变大 <p>Do you like to ride a bicycle?</p> p:first-letter{ font-size: 34px; }

  9. Eclipse闪退解决办法

    解决方式: 1.通过在命令行中输入“where java”,找到除jdk目录下的所有java相关程序,直接删掉(一般会在C:\WINDOWS\system32下) 2.内存不足,打开Eclipse目录 ...

  10. iOS 面试题 1

    1.    简述OC中内存管理机制.与retain配对使用的方法是dealloc还是release,为什么?需要与alloc配对使用的方法是dealloc还是release,为什么?readwrite ...