[抄题]:

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. PythonStudy——文件操作 File operation

    # 文件:就是硬盘的一块存储空间 # 1.使用文件的三步骤: # 打开文件- 得到文件对象:找到数据存放在硬盘的位置,让操作系统持有该空间,具有操作权# 硬盘空间 被 操作系统持有# 文件对象f 被 ...

  2. windows 2008R2系统程序运行提示无法定位程序输入点ucrtbase.terminate

    1.用python写了个脚本,打成exe程序,在一些机器上正常运行,再另外一些机器上运行提示 无法定位程序输入点ucrtbase.terminate 应该是缺少库文件支持 2.网上搜了下.https: ...

  3. nodeppt:网页版 PPT

    资料 网址 github https://github.com/ksky521/nodeppt 网页版PPT(nodeppt 的介绍) http://deliazhi.com/2017/03/31/W ...

  4. Lock的lockInterruptibly()方法

    lockInterruptibly()方法能够中断等待获取锁的线程.当两个线程同时通过lock.lockInterruptibly()获取某个锁时,假若此时线程A获取到了锁,而线程B只有等待,那么对线 ...

  5. LiveBindings如何绑定一个对象(转)

    原文 http://www.malcolmgroves.com/blog/?p=1084 一.新建VCL工程加入TAdapterBingSource控件 二.定一个TPerson类 MyPerson ...

  6. 【算法和数据结构】_14_小算法_Blank字符替换

    /* 本程序用来将输入的制表符替换为\t, 而将退格替换为\b, 将反斜杠替换为\\ */ #include <stdio.h> #include <stdlib.h> typ ...

  7. USB3.0及NVME SSD安装WIN7X64

    USB3.0及NVME SSD安装WIN7X64https://tieba.baidu.com/p/4822034273?pn=1所有的人都是菜鸟过来的,不过有些人懂得自己动手找到答案:有些人则是懒得 ...

  8. DBus send byte array over gdbus ----Send dbus data

    遇到一个问题,如何通过dbus传送uint8数组元素 有3种方法, 1.直接传 ay 2.传  a(y) 3.xml定义为 ay,但是通过annotation 强行将 guchar 转为GVarian ...

  9. TypeScript type 类型别名

    //6,类型别名 /**类型别名不能出现在声明右侧的任何地方. * 接口 vs. 类型别名 * 另一个重要区别是类型别名不能被extends和implements(自己也不能extends和imple ...

  10. Linux系统重置root用户密码

    Linux系统重置root用户密码 作者:Eric 微信:loveoracle11g 查看系统版本是不是RHEL7 [root@zhouwanchun ~]# cat /etc/redhat-rele ...