2.27 第一轮复习(2. 两数相加,11. 盛最多水的容器 剑指 Offer 53 - II. 0~n-1中缺失的数字 21. 合并两个有序链表)

题一:2. 两数相加(链表题)

链接

题目链接:

视频总结

编程思路

Me:
  1. 取出数字,然后相加,然后放回链表

力扣实战

思路一:

 # 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. 盛最多水的容器(数组)

链接

题目链接:

视频总结

关键点

  1. 两层for循环,暴力求解,复杂度为恩芳,可能会超时
  2. 根据题目的提示二,首末指针,记录面积,移动小的指针

编程思路

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中缺失的数字

链接

题目链接:

视频总结

关键点

  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
            

递归复杂度分析:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值