【力扣hot100】刷题笔记Day3

前言

  • 以撒真是一不小心就玩太久了,终于解锁骨哥嘞,抓紧来刷题,今天是easy双指针!

283. 移动零 - 力扣(LeetCode)

  • 一个指针遍历,一个指针用于交换前面的0
python 复制代码
class Solution(object):
    def moveZeroes(self, nums):
        pre = 0  # 用于交换前面的0
        for i in range(len(nums)):
            if nums[i] != 0:
                nums[i], nums[pre] = nums[pre], nums[i]  # 交换
                pre += 1
        return nums

11. 盛最多水的容器 - 力扣(LeetCode)

  • 左右双指针,柱子较矮的往中间靠拢(因为最大盛水容器受限于最矮柱子)
python 复制代码
class Solution(object):
    def maxArea(self, height):
        l, r = 0, len(height) - 1  # 首尾双指针
        res = 0
        while l != r:  # 往中间靠拢
            # 记录最大雨水量
            res = max(res, min(height[l], height[r]) * (r - l))  
            if height[l] < height[r]:  # 较矮的往中间移,相等随便
                l += 1
            else:
                r -= 1
        return res

15. 三数之和 - 力扣(LeetCode)

  • 基本沿袭之前三数之和C++版本的思路,排序 + 双指针 +剪枝去重
python 复制代码
class Solution(object):
    def threeSum(self, nums):
        res = list()
        nums.sort()  # 先排序
        n = len(nums)
        for i in range(n):
            if nums[i] > 0:  # 大于0后面不可能相加等于0了,直接break
                break
            if i > 0 and nums[i] == nums[i - 1]:  # i去重
                continue
            l, r = i + 1, n - 1  # 前后双指针
            while l < r:
                sum = nums[i] + nums[l] + nums[r]
                if sum < 0:
                    l += 1
                elif sum > 0:
                    r -= 1
                else:
                    res.append([nums[i], nums[l], nums[r]])
                    while l < r and nums[l + 1] == nums[l]:  # l去重
                        l += 1
                    while l < r and nums[r - 1] == nums[r]:  # r去重
                        r -= 1
                    l += 1
                    r -= 1
        return res

42. 接雨水 - 力扣(LeetCode)

  • 灵神的题解很清晰,除了之前刷的单调栈的思路,这里用相向双指针前后更新最高柱子
python 复制代码
class Solution:
    def trap(self, height: List[int]) -> int:
        res = l_max = r_max = 0
        l, r = 0, len(height) - 1  # 相向双指针
        while l < r:
            l_max = max(l_max, height[l])  # 左边最高
            r_max = max(r_max, height[r])  # 右边最高
            if l_max < r_max:  # 单格雨水受限于两侧较小的最高柱
                res += l_max - height[l]
                l += 1
            else:
                res += r_max - height[r]
                r -= 1
        return res

后言

  • 有前面的基础,刷起题目一下子就能懂啦,而且python写起来还是很顺手滴
相关推荐
程序员-King.6 分钟前
day134—快慢指针—环形链表(LeetCode-141)
算法·leetcode·链表·快慢指针
Swift社区7 分钟前
LeetCode 376 摆动序列
算法·leetcode·职场和发展
高洁0114 分钟前
AIGC技术与进展(1)
深度学习·算法·机器学习·transformer·知识图谱
未来之窗软件服务15 分钟前
计算机等级考试——二叉树考点和坑——东方仙盟
算法·二叉树·计算机软考·仙盟创梦ide·东方仙盟
中屹指纹浏览器20 分钟前
指纹浏览器底层沙箱隔离技术实现原理与架构优化
经验分享·笔记
张张努力变强27 分钟前
C++类和对象(一):inline函数、nullptr、类的定义深度解析
开发语言·前端·jvm·数据结构·c++·算法
且去填词1 小时前
深入理解 GMP 模型:Go 高并发的基石
开发语言·后端·学习·算法·面试·golang·go
Hcoco_me1 小时前
大模型面试题76:强化学习中on-policy和off-policy的区别是什么?
人工智能·深度学习·算法·transformer·vllm
企业对冲系统官1 小时前
大宗商品风险对冲系统统计分析功能的技术实现
运维·python·算法·区块链·github·pygame
ValhallaCoder1 小时前
Day48-单调栈
数据结构·python·算法·单调栈