第四天:
做了两题 零钱兑换1 2 分别对应了 322 518
两题都是动态规划 背包问题, 要点还是跟前面的动态规划一样 建立一个dp矩阵,注意初始值的设定,之后维护这矩阵即可,注意转移条件,背包问题应该要不停的减少价值作为条件。
class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
n = len(coins)
dp = [[amount+1]*(amount+1) for _ in range(n+1)]
dp[0][0] = 0
for i in range(1, n+1):
for j in range(amount+1):
if j < coins[i-1]:
dp[i][j] = dp[i-1][j]
else:
dp[i][j] = min(dp[i-1][j], dp[i][j-coins[i-1]]+1)
ans = dp[n][amount]
return ans if ans != amount+1 else -1
class Solution:
def change(self, amount: int, coins: List[int]) -> int:
n = len(coins)
dp = [[0]*(amount+1) for _ in range(n+1)]
dp[0][0] = 1
for i in range(1, n+1):
for j in range(amount+1):
if coins[i-1] > j:
dp[i][j] = dp[i-1][j]
else:
dp[i][j] = dp[i-1][j] + dp[i][j-coins[i-1]]
return dp[n][amount]
第五天:
两个链表题,算是重新想起了链表是啥,反转链表和排序链表 206 148
做链表题 快慢指针 一定要自己debug 或者画图 不然不太容易理解:
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def sortList(self, head: ListNode) -> ListNode:
if not head or not head.next: return head # termination.
# cut the LinkedList at the mid index.
slow, fast = head, head.next
while fast and fast.next:
fast, slow = fast.next.next, slow.next
mid, slow.next = slow.next, None # save and cut.
# recursive for cutting.
left, right = self.sortList(head), self.sortList(mid)
# merge `left` and `right` linked list and return it.
h = res = ListNode(0)
while left and right:
if left.val < right.val: h.next, left = left, left.next
else: h.next, right = right, right.next
h = h.next
h.next = left if left else right
return res.next
第六天:
三题 一个数组k大元素215,两个链表:合并K个升序和K个一维翻转链表23 25
数组题主要是复习了一下排序,无脑快排 不想写的话可以直接sorted
class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
def qSort(self, nums, left, right):
temp = nums[(left+right) // 2]
i, j = left, right
while i <= j:
while nums[i] < temp: i+=1
while nums[j] > temp: j-=1
if i <= j:
nums[i], nums[j] = nums[j], nums[i]
i+=1
j-=1
if i < right: qSort(self, nums, i, right)
if j > left: qSort(self, nums, left, j)
qSort(self, nums, 0, len(nums)-1)
return nums[len(nums)-k]
第一个链表题主要是递归和归并排序:递归记得很难很难,就算是刷题很久的人,也很容易做不出,所以不用急要多做,递归和双指针不明白一定用pycharm调试debug 看看一行行顺序执行和参数变化再理解:
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]:
if not lists: return
n = len(lists)
return self.merge(lists, 0, n-1)
def merge(self, lists, left, right):
if (left == right):
return lists[left]
mid = (left + right) // 2
l1 = self.merge(lists, left, mid)
l2 = self.merge(lists, mid+1, right)
return self.mergetwolst(l1, l2)
def mergetwolst(self, l1, l2):
if not l1: return l2
if not l2: return l1
if l1.val < l2.val:
l1.next = self.mergetwolst(l1.next, l2)
return l1
else:
l2.next = self.mergetwolst(l1, l2.next)
return l2
反转数组那个 这种整个转过来的 栈啊啊啊!!!入栈出栈不就好了!!!:
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reverseKGroup(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
dummy = ListNode(0)
p = dummy
while True:
count = k
stack = []
tmp = head
while count and tmp:
stack.append(tmp)
tmp = tmp.next
count -= 1
if count:
p.next = head
break
while stack:
p.next = stack.pop()
p = p.next
p.next = tmp
head = tmp
return dummy.next