移除元素
class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
low, fast = 0, 0
while fast < len(nums):
if nums[fast] != val:
nums[low] = nums[fast]
low += 1
fast += 1
return low
反转字符串
class Solution:
def reverseString(self, s: List[str]) -> None:
"""
Do not return anything, modify s in-place instead.
"""
left = 0
right = len(s) - 1
while left < right:
s[left], s[right] = s[right], s[left]
left += 1
right -= 1
return s
替换空格
class Solution:
def replaceSpace(self, s: str) -> str:
res = list(s)
for i in range(len(res)):
if res[i] == ' ':
res[i] = '%20'
return ''.join(res)
翻转字符串里的单词
class Solution:
def reverseWords(self, s: str) -> str:
def rm_space(s):
left, right =0, len(s) - 1
res = []
while left < right and s[left] == ' ': left += 1
while left < right and s[right] == ' ': right -= 1
while left <= right:
if s[left] != ' ':
res.append(s[left])
elif res[-1] != ' ':
res.append(s[left])
left += 1
return res
def reverse(s, left, right):
while left < right:
s[left], s[right] = s[right], s[left]
left += 1
right -= 1
return s
def reverse_each_word(s):
left = 0
right = 0
while right < len(s):
while right < len(s) and s[right] != ' ':
right += 1
reverse(s, left, right - 1)
left = right + 1
right += 1
return s
res = list(s)
res = rm_space(res)
res = reverse(res, 0, len(res) - 1)
res = reverse_each_word(res)
return ''.join(res)
翻转链表
class Solution:
def reverseList(self, head: ListNode) -> ListNode:
pre = None
cur = head
while cur:
tmp = cur.next
cur.next = pre
pre = cur
cur = tmp
return pre
删除链表的倒数第N个节点
记得搞个虚拟头
class Solution:
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
dummy_head = ListNode()
dummy_head.next = head
low, fast = dummy_head, dummy_head
for _ in range(n + 1):
fast = fast.next
while fast:
fast = fast.next
low = low.next
low.next = low.next.next
return dummy_head.next
链表相交
class Solution:
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
dummy_headA, dummy_headB = ListNode(), ListNode()
dummy_headA.next = headA
dummy_headB.next = headB
def get_len(head):
cur = ListNode()
cur.next = head
num = 0
while cur:
num += 1
cur = cur.next
return num
len_A = get_len(headA)
len_B = get_len(headB)
if len_A > len_B:
for i in range(len_A - len_B):
dummy_headA = dummy_headA.next
else:
for i in range(len_B - len_A):
dummy_headB = dummy_headB.next
while dummy_headA:
if dummy_headA == dummy_headB:
return dummy_headA
else:
dummy_headA = dummy_headA.next
dummy_headB = dummy_headB.next
return None
环形链表
注意这里的 while fast and fast.next, 如果不加fast最后可能会没有next
class Solution:
def detectCycle(self, head: ListNode) -> ListNode:
low, fast = head, head
while fast and fast.next:
low = low.next
fast = fast.next.next
if low == fast:
cur_A = head
cur_B = fast
while cur_A != cur_B:
cur_A = cur_A.next
cur_B = cur_B.next
return cur_A
return None
三数之和
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
nums.sort()
res = []
for i in range(len(nums)):
left = i + 1
right = len(nums) - 1
if nums[i] > 0:
break
if i >= 1 and nums[i] == nums[i - 1]:
continue
while left < right:
target = nums[i] + nums[left] + nums[right]
if target < 0:
left += 1
elif target > 0:
right -= 1
else :
res.append([nums[i], nums[left], nums[right]])
while left != right and nums[left] == nums[left + 1]: left += 1
while left != right and nums[right] == nums[right - 1]: right -= 1
left += 1
right -= 1
return res
四数之和
class Solution:
def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
res = []
n = len(nums)
nums.sort()
for i in range(n):
if i >= 1 and nums[i] == nums[i-1]:
continue
for j in range(i+1, n):
if j > i + 1 and nums[j] == nums[j-1]:
continue
left = j + 1
right = n - 1
while left < right:
_sum = nums[i] + nums[j] + nums[left] + nums[right]
if _sum > target:
right -= 1
elif _sum < target:
left += 1
else:
res.append([nums[i], nums[j], nums[left], nums[right]])
while left != right and nums[left] == nums[left + 1]: left += 1
while left != right and nums[right] == nums[right - 1]: right -= 1
left += 1
right -= 1
return res
感谢阅读