删除链表倒数第N个节点
class Solution:
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
p = q = head
for _ in range(n):
q = q.next
if not q:
return p.next
while q.next:
p, q = p.next, q.next
p.next = p.next.next
return head
最长回文子串
class Solution:
def longestPalindrome(self, s: str) -> str:
res = ""
for i in range(len(s)):
tmp = self.helper(s, i, i)
if len(tmp) > len(res):
res = tmp
tmp = self.helper(s, i, i+1)
if len(tmp) > len(res):
res = tmp
return res
def helper(self, s, l, r):
while l>=0 and r < len(s) and s[l]==s[r]:
l -= 1
r += 1
return s[l+1:r]
无重复字符的最长子串
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
left = right = res = 0
char = dict()
for right in range(len(s)):
if s[right] in char:
left = max(left, char[s[right]] + 1)
char[s[right]] = right
res = max(res, right - left + 1)
return res