一、判断一个链表是否为回文结构
输入:[1,2,2,1] 返回值:true 说明:1->2->2->1
解法:利用栈结构
class Solution:
def isPail(self , head ):
# write code here
p = head
stack = []
while p:
stack.append(p.val)
p = p.next
q = head
while q:
tmp = stack.pop()
if tmp == q.val:
q = q.next
else:
return False
return True
二、回文数字
在不使用额外的内存空间的条件下判断一个整数是否是回文数字
输入: 121 返回值: true
class Solution:
def isPalindrome(self , x ):
# write code here
if x<0:
return False
n1 = 0
n2 = x
while n2>0:
n1 = n1*10 + n2%10
n2 = n2//10
return n1 == x
三、最长回文子串
1、中心扩散法。2、动态规划- 转换为最长公共子串解法。
四、最长回文子序列
动态规划— 转换为 最长公共子序列解法