题解 | #最长回文子序列#
最长回文子序列
http://www.nowcoder.com/practice/c7fc893654b44324b6763dea095ceaaf
动态规划
public int longestPalindromeSubSeq (String s) { int len = s.length(); if(len == 0){ return 0; } // i - j 的最长回文子序列的长度 int[][] dp = new int[len][len]; for(int i = len - 1 ; i >= 0 ; i--){ for(int j = i ; j < len ; j++){ if(i == j){ dp[i][j] = 1; }else if(s.charAt(i) == s.charAt(j)){ dp[i][j] = dp[i+1][j-1] + 2; }else{ dp[i][j] = Math.max(dp[i+1][j] ,dp[i][j-1]); } } } return dp[0][len-1]; }