[抄题]:

Given a string s, find the longest palindromic subsequence's length in s. You may assume that the maximum length of s is 1000.

Example 1:
Input:

"bbbab"

Output:

4

One possible longest palindromic subsequence is "bbbb".

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

for i = s.length() - 1; i >= 都可以成为找bug的对象

[思维问题]:

忘记回文串dp怎么写了。而且这道题自称坐标型 && index顺序略微奇葩。

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

dp回文串就是:加2或者取两者中较大值

[复杂度]:Time complexity: O(方) Space complexity: O(方)

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

[潜台词] :

class Solution {
public int longestPalindromeSubseq(String s) {
//cc
if (s == null || s.length() == 0) return 0; //ini n, dp[][] == 1
int n = s.length();
int[][] dp = new int[n][n];
for (int i = n - 1; i >= 0; i--) {
//ini
dp[i][i] = 1;
for (int j = i + 1; j < n; j++) {
//discuss in 2 cases: dp[i][j] = dp[i + 1][j - 1] + 2 or max(dp[i][j - 1], dp[i + 1][j]);
if (s.charAt(i) == s.charAt(j)) {
dp[i][j] = dp[i + 1][j - 1] + 2;
}else {
dp[i][j] = Math.max(dp[i][j - 1], dp[i + 1][j]);
}
}
} return dp[0][n - 1];
}
}

516. Longest Palindromic Subsequence最长的不连续回文串的长度的更多相关文章

  1. [LeetCode] 516. Longest Palindromic Subsequence 最长回文子序列

    Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...

  2. 【LeetCode】516. Longest Palindromic Subsequence 最长回文子序列

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 代码 刷题心得 日期 题目地址:https://le ...

  3. 516 Longest Palindromic Subsequence 最长回文子序列

    给定一个字符串s,找到其中最长的回文子序列.可以假设s的最大长度为1000. 详见:https://leetcode.com/problems/longest-palindromic-subseque ...

  4. LN : leetcode 516 Longest Palindromic Subsequence

    lc 516 Longest Palindromic Subsequence 516 Longest Palindromic Subsequence Given a string s, find th ...

  5. [LeetCode] Longest Palindromic Subsequence 最长回文子序列

    Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...

  6. 516. Longest Palindromic Subsequence

    Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...

  7. LC 516. Longest Palindromic Subsequence

    Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...

  8. [leetcode]516. Longest Palindromic Subsequence最大回文子序列

    Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...

  9. [LeetCode] Length of Longest Fibonacci Subsequence 最长的斐波那契序列长度

    A sequence X_1, X_2, ..., X_n is fibonacci-like if: n >= 3 X_i + X_{i+1} = X_{i+2} for all i + 2 ...

随机推荐

  1. Python2和Python3关于reload()用法的区别

    Python2 中可以直接使用reload(module)重载模块. Pyhton3中需要使用如下两种方式: 方式(1) >>> from imp >>> imp. ...

  2. Xshell添加ssh隧道SOCKS代理

    Xshell是一个功能强大的终端模拟器,支持SSH,SFTP.TELNET.RLOGIN和SERIAL 下载地址:http://www.netsarang.com/products/xsh_overv ...

  3. jQeury 批量删除

    /*批量删除*/ function datadel(){ var ids = new Array(); $("input[name='batch']:checked").each( ...

  4. flask-appbuilder +echarts 展示数据笔记

    pip install flask-appbuilder fabmanager create-app cd newapp fabmanager create-admin fabmanager run ...

  5. jstack命令定位java程序CPU利用率高的代码位置

    高手是怎么使用jstack精确找到异常代码的(java程序CPU利用率高的情况) 请jstack神器来帮忙 本文介绍Linux环境下使用jstack定位问题的秘笈1.[top命令]找到CPU利用率持续 ...

  6. __slots__用法

    class Test(object): __slots__ = ("name","age") t = Test() t.name = "老王" ...

  7. Python微信

    """ Description: 需要提供以下三个信息,在申请到的微信企业号当中可以找到 agentid corpid corpsecret Author:Nod Dat ...

  8. 知识点:SQL中char、varchar、text区别

    Char为定长,varchar,text为变长. 1.CHAR.CHAR存储定长数据很方便,CHAR字段上的索引效率级高,比如定义char(10),那么不论你存储的数据是否达到了10个字节,都要占去1 ...

  9. Alsa aplay S8 U8 S16_LE S16_BE U16_LE U16_BE格式

    举个例子 aplay -r 16000 -f S16_LE -D hw:0,0 -c 2 -d 3 ~/Private/Private_Tools/02_ALSA_Learning/left_1k_r ...

  10. [SQL]sql server中如何直接查询存储过程EXEC返回的结果集?

    Declare @T Table (iDay VARCHAR(),iNum DECIMAL(,),yuxiang DECIMAL(,)) Insert @T --EXEC [dbo].[BSP0101 ...