Leetcode 5:最长回文子串
超详细的讲解在这里~
我只能说看得我眼都花了,看完一遍之后脑子一片空白。。。(我是谁?我在干什么?)
大致地看明白以上的讲解,还是直接看程序比较明白!并且简单地梳理了一下前面的内容,讲了这么多,其实最关键的就是:
- 分四种情况考虑:
- 当
L[currentLeftPosition] < centerRightPosition - currentRightPosition时:
L[currentRightPosition] = L[currentLeftPosition]
- 当
L[currentLeftPosition] = centerRightPosition - currentRightPosition(对应第一个条件) and centerRightPosition = 2*N时:
L[currentRightPosition] = L[currentLeftPosition]
此时,整个字符串都是回文串。
- 当
L[currentLeftPosition] = centerRightPosition - currentRightPosition(对应第一个条件) and centerRightPosition < 2*N时:
L[currentRightPosition] >= L[currentLeftPosition]
在这种情况下,有可能出现右侧回文扩张,因此右侧回文串的长度至少与左侧回文串的长度一样长。
- 当
L[currentLeftPosition] >= centerRightPosition - currentRightPosition时:
L[currentRightPosition]> centerRightPosition - currentRightPosition
在这种情况下,右侧回文串的长度至少与(centerRightPosition-currentRightPosition)一样长,并且有可能出现右侧回文回文扩展。
** 代码其实很短,只是注释写得比较多hh
def findLongestPalindromicString(text):
N = len(text)
if N == 0:
return
N = 2*N+1 # Position count
L = [0] * N
L[0] = 0
L[1] = 1
C = 1 # centerPosition
R = 2 # centerRightPosition
i = 0 # currentRightPosition
iMirror = 0 # currentLeftPosition
maxLPSLength = 0
maxLPSCenterPosition = 0
start = -1
end = -1
diff = -1
# Uncomment it to print LPS Length array
# printf("%d %d ", L[0], L[1]);
for i in range(2,N): # 以currentRightPosition滑动
# currentLeftPosition = 2*centerPosition - currentRightPosition
iMirror = 2*C-i
# 先初始化当前currentRightPosition的值为0
L[i] = 0
# diff = centerRightPosition - currentRightPosition
diff = R - i
# diff > 0 说明currentRightPosition处于当前centerPosition的回文子串中
if diff > 0:
L[i] = min(L[iMirror], diff)
# 由分析可知,此时currentRightPosition对应的LPS长度:
# 要么对称 =>等于相应的左边currentLeftPosition的LPS长度
# 要么等于(回文子串的右边边界-当前位置)的长度
# 二者最小的一个
# centerRightPosition = centerPosition + d
# centerLeftPosition = centerPosition - d
# 仅对范围外扩展回文的情况进行考虑,(i + L[i])范围内的情况不用考虑。
# 左边是要先算好的,所以扩展情况只需要考虑右边。
# 当找到奇数位置时,将进行比较并且LPS长度将增加1。
# 当找到偶数位置时,不进行比较并且LPS长度将增加1(因此总体而言,左侧和右侧的一个奇数位置和一个偶数位置将使LPS长度增加两倍)
try:
while ((i + L[i]) < N and (i - L[i]) > 0) and \ # (i + L[i])以currentRightPosition为中心点的右边界,(i - L[i])相应的左边界
(((i + L[i] + 1) % 2 == 0) or \ # 偶数位置,即将原字符串扩充后'|'的位置
(text[(i + L[i] + 1) // 2] == text[(i - L[i] - 1) // 2])): #奇数位置
L[i]+=1
except Exception as e:
pass
if L[i] > maxLPSLength: # Track maxLPSLength
maxLPSLength = L[i]
maxLPSCenterPosition = i
# 如果当前位置的右边界已经超出了原来centerPosition的右边界,说明当前位置的回文子串长度要大于原centerPosition
# 所以将当前位置更新为新的centerPosition,同理,当前的右边界相应更新为新的右边界
if i + L[i] > R:
C = i
R = i + L[i]
# Uncomment it to print LPS Length array
# printf("%d ", L[i]);
start = (maxLPSCenterPosition - maxLPSLength) // 2
end = start + maxLPSLength - 1
print("LPS of string is " + text + " : " + text[start:end+1])
其他求解最长回文子串的方法:
方法二:
class Solution(object):
def longestPalindrome(self, s):
"""
:type s: str
:rtype: str
"""
if not s:
return ""
l = len(s)
dp = [[0]*l for i in range(l)] #用来计算当前回文串的长度
maxn = 1
res = s[0]
for j in range(l):
dp[j][j] = 1
for i in range(j):
if s[i] == s[j]:
if (j-i) == 1:
#如果i,j位置相差1,说明是偶数中心点的情况,回文子串长度为2
dp[i][j] = 2
elif dp[i+1][j-1]:
#如果当前位置内部是回文情况,那么加上当前位置对应的两个,回文子串长度应+2
dp[i][j] = dp[i+1][j-1] + 2
if dp[i][j] > maxn:
maxn = dp[i][j]
res = s[i:j+1]
return res
方法三:
class Solution(object):
def longestPalindrome(self, s):
"""
:type s: str
:rtype: str
"""
if not s:
return ""
maxl = 1 #初始最长度为1,即字母自身的长度
l = len(s)
res = s[0]
for i in range(1,l): # 逐个字母依次检查是否有满足当前最大长度的子串为回文子串
odd = s[i-maxl-1:i+1]
even = s[i-maxl:i+1]
if i-maxl-1>=0 and odd == odd[::-1]:
maxl += 2
res = odd
if i-maxl >= 0 and even == even[::-1]:
maxl += 1
res = even
return res
Leetcode 4:寻找两个有序数组的中位数
####### 要点:
把2个数组看做一个虚拟的数组A,目前有2m+2n+2个元素,割在m+n+1处,所以我们只需找到m+n+1位置的元素和m+n+2位置的元素就行了。
左边:A[m+n+1] = Max(L1+L2)
右边:A[m+n+2] = Min(R1+R2)
在虚拟数组A中:
Li = (Ci-1)/2
Ri = Ci/2
- L1>R2,把C1减小,C2增大。—> C1向左二分
- L2>R1,把C1增大,C2减小。—> C1向右二分
class Solution(object):
def findMedianSortedArrays(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: float
"""
l1 = len(nums1)
l2 = len(nums2)
if l1 > l2: # 保证数组1一定最短
return self.findMedianSortedArrays(nums2,nums1)
high1 = 2*l1 #虚拟地加了'#'之后nums1的长度变为2*l1
low1 = 0
while low1 <= high1:
c1 = (low1 + high1)//2 #找到nums1中间的位置作为nums1“割”的位置
c2 = l1 + l2 - c1 # 虚拟加了'#'之后,nums1+nums2整个数组集的中间位置是k=n+m,所以nums2的中间位置(即nums2“割”的位置)为c2=k-c1
# c1 == 0: 说明nums1均大于nums2,中位数位于nums2中
# 按照虚拟扩展数组的规律:割的左边最后一个元素的坐标为Li = (Ci-1)/2 ;割的右边第一个元素的坐标为Ri = Ci/2
L1 = float('-inf') if c1 == 0 else nums1[(c1-1)//2]
R1 = float('inf') if c1 == 2*l1 else nums1[c1//2]
L2 = float('-inf') if c2 == 0 else nums2[(c2-1)//2]
R2 = float('inf') if c2 == 2*l2 else nums2[c2//2]
if L1 > R2:
high1 = c1 - 1
elif L2 > R1:
low1 = c1 + 1
else:
break # 此时找到了k=m+n时的合适分割位置
return (max(L1,L2)+min(R1,R2))/2.0
本文详细解析LeetCode上的经典题目,包括最长回文子串和寻找两个有序数组的中位数。通过深入浅出的讲解,帮助读者理解并掌握解决这类问题的技巧。

346

被折叠的 条评论
为什么被折叠?



