Skip to content

Latest commit

 

History

History
47 lines (44 loc) · 2.56 KB

七月palindorme.md

File metadata and controls

47 lines (44 loc) · 2.56 KB

pay attention to subsequence and substring

  • longest-palindromic-subsequence Solution 这个是subset 区别这个和subsequence dp 数组的定义是 dp[i][j]=x的定义是,string[i:j]内的最长回文子序列的长度。 二维dp 所以这个题目迭代的时候要从最后开始迭代,j却要从 range(i+1,n)内迭代。 if the length of string is 7, the last index of the string is 6 Then the sequence of (i,j) will be 5,6 4,5 4,6 3,4 -word-break Solution 一维dp数组,返回值是true or false

-word-break-ii Solution 这个用的是回溯 def backtrack(start,track), 选择列表是 word list

-palindrome-number Solution 这是左右指针的问题 不过这个可以用来当判断一个序列是否是 回文串 palindrome 。It ask you if a number is a palindrome number

for i in range(n-1,-1,-1):
    for j in range(i+1,n):
        dp[i][j]=(s[i]==s[j]) and dp[i+1][j-1]
Input: s = "aaa"
Output: 6
Explanation: Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa".
Output: [["a","a","b"],["aa","b"]]

utilize the dp[i][j] palindrome first then backtracking, for j in range(start,n), determine if dp[start][j] is a palindrome

for i in  range(n-1,-1,-1):
  for j in range(i+1,n):
    dp[i][j] = (s[i] == s[j]) and dp[i + 1][j - 1]