题一:2. 两数相加(链表题)
链接
视频总结
编程思路
Me:
- 取出数字,然后相加,然后放回链表
力扣实战
思路一:
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
# 取出数字,然后相加,然后放回链表
length1,length2 = 0,0
num1,num2 = 0,0
cur1 = l1
cur2 = l2
while cur1: #取出数字1
num1 += cur1.val *(10**length1)
length1+=1
cur1 = cur1.next
while cur2: #取出数字2
num2 += cur2.val *(10**length2)
length2+=1
cur2 = cur2.next
if length1==1 and length2==1 and not l1.val and not l2.val: #去除特殊情况
return l1
tem = num1+num2
dummy = ListNode(0)
cur = dummy
while tem: #放回链表
val = tem%10
tem = tem//10
tem1 = ListNode(val)
cur.next = tem1
cur = cur.next
return dummy.next
# 反思1:链表的操作没考到,违背了初衷
思路二: 确实牛皮,记录下进位值,直接参与下次的值运算
class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
head = ListNode()
curr = head
carry = val = 0
while carry or l1 or l2:
val = carry
if l1:
l1, val = l1.next, l1.val + val
if l2:
l2, val = l2.next, l2.val + val
carry, val = divmod(val, 10)
curr.next = ListNode(val)
curr = curr.next
return head.next
文档总结
1. divmod(a,b)方法返回的是a/b(取整)以及a对b的余数,divmod(7,3)返回的结果是(2,1)
题二:11. 盛最多水的容器(数组)
链接
视频总结
关键点
- 两层for循环,暴力求解,复杂度为恩芳,可能会超时
- 根据题目的提示二,首末指针,记录面积,移动小的指针
编程思路
Me:
卡尔:
力扣实战
思路一:
class Solution:
def maxArea(self, height: List[int]) -> int:
# 两层for循环,暴力求解,复杂度为恩芳,可能会超时
# 根据题目的提示二,首末指针,记录面积,移动小的指针
left = 0
right = len(height)-1
res = 0
while left<right:
res = max((right-left)*min(height[left],height[right]),res)
if height[left]>=height[right]:
right-=1
else:
left+=1
return res
# 反思1:
写法二:
class Solution:
def maxArea(self, height: List[int]) -> int:
i, j, res = 0, len(height) - 1, 0
while i < j:
if height[i] < height[j]:
res = max(res, height[i] * (j - i))
i += 1
else:
res = max(res, height[j] * (j - i))
j -= 1
return res
题三:剑指 Offer 53 - II. 0~n-1中缺失的数字
链接
视频总结
关键点
- 哈希表,双指针
return list(hashtable.keys())[list(hashtable.values()).index(0)] #奈斯,复习哈希表由val返回key
力扣实战
思路一:哈希表存储
class Solution:
def missingNumber(self, nums: List[int]) -> int:
# 思路:字典实现哈希表,查询值为零对应的key
n = len(nums)
hashtable = {}
for i in range(n+1):
hashtable[i]=0
for num in nums:
hashtable[num]=1
return list(hashtable.keys())[list(hashtable.values()).index(0)] #奈斯,复习哈希表由val返回key
# 反思1:
思路二:双指针更奈斯,
class Solution:
def missingNumber(self, nums: List[int]) -> int:
# 思路:双指针,一左一右,一个预期中间值和实际中间值,对比大小确定区间
# 上面思路对,但是后面的实现复杂了,其实就是元素值是否等于索引,等于就说明在右边区间
i,j=0,len(nums)
while i <j:
mid = (i+j)//2
if nums[mid]==mid:
i = mid+1
else:
j = mid
return i
反思:注意边界问题(j取len还是len-1)和边界问题,比如只有一个数,缺的数在最右边?
题四:21. 合并两个有序链表
链接
视频总结
力扣实战
思路一:迭代
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
dummy = ListNode(0)
cur = dummy
if not list1:
return list2
if not list2:
return list1
while list1 and list2:
if list1.val <list2.val:
cur.next =list1
cur = cur.next
list1 = list1.next
else:
cur.next =list2
cur = cur.next
list2 = list2.next
if list1: #有一个为空时再单独考虑
cur.next = list1
else:
cur.next=list2
return dummy.next
# 反思1:
思路二:递归大法 时间为欧m+n
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
# 递归,直接自己调用自己
if not list1:
return list2
if not list2:
return list1
if list1.val<=list2.val:
list1.next = self.mergeTwoLists(list1.next,list2)
return list1
else:
list2.next = self.mergeTwoLists(list1,list2.next)
return list2
递归复杂度分析: