【HOT100】DAY2

盛最多水的容器

双指针,通过指针大小控制循环;更小的高参与迭代。

python 复制代码
class Solution:
    def maxArea(self, height: List[int]) -> int:
        left = 0
        right = len(height) - 1
        max_area = 0
        while left < right:
            high = min(height[left], height[right])
            width = right - left
            current_area = high * width
            max_area = max(current_area, max_area)
            if height[left] < height[right]:
                left += 1
            else:
                right -= 1
        return max_area

三数之和

循环,i作为一个指针;循环内双指针,左右分别剪枝/去重;通过三数之和的大小决定双指针移动方向

python 复制代码
class Solution:
    def threeSum(self, nums: list[int]) -> list[list[int]]:
        n = len(nums)
        res = []
        if n < 3:
            return res
        
        nums.sort()
        for i in range(n):
            if nums[i] > 0:
                break
            if i > 0 and nums[i] == nums[i - 1]:
                continue
            
            left = i + 1
            right = n - 1
            while left < right:
                sum_three = nums[i] + nums[left] + nums[right]
                if sum_three == 0:
                    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
                elif sum_three < 0:
                    left += 1
                else:
                    right -= 1
        return res

接雨水

每次只做一步动作,接水和移动指针错开,左右也错开。移动的下一步要是比当前大,就更新大小,若比当前小,二者差值就是雨水数量。

python 复制代码
class Solution:
    def trap(self, height: List[int]) -> int:
        n = len(height)
        if n < 3:
            return 0
        
        left = 0
        right = n - 1
        left_max = 0
        right_max = 0
        total_water = 0

        while left < right:
            if height[left] < height[right]:
                if height[left] >= left_max:
                    left_max = height[left]
                else:
                    total_water += left_max - height[left]
                left += 1
            else:
                if height[right] >= right_max:
                    right_max = height[right]
                else:
                    total_water += right_max - height[right]
                right -= 1
        
        return total_water

无重复字符的最长子串

哈希+滑动窗口,用s的序号当作右指针并参与循环。

python 复制代码
class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        hash_map = {}
        max_length = 0
        left = 0
        for right, char in enumerate(s):
            if char in hash_map and hash_map[char] >= left:
                left = hash_map[char] + 1
            hash_map[char] = right
            current_length = right - left + 1
            max_length = max(current_length, max_length)
        return max_length
相关推荐
用户03321266636721 分钟前
使用 Python 从零创建 Word 文档
python
Csvn5 小时前
Python 两大经典坑点 —— 可变默认参数 & 闭包延迟绑定
后端·python
曲幽6 小时前
别再用网页翻译看源码了!你的私人翻译神器LibreTranslate,部署避坑指南来了
python·docker·web·pot·translate·libretranslate·arogstranslate
猿人谷7 小时前
不只是 CPU 阈值:STAR 如何用 GAT + Transformer 做容器级自动扩缩容?
人工智能·算法
用户556918817538 小时前
#从脚本到独立程序:Python + Playwright 批量抓取的完整踩坑记录
python·自动化运维
复杂网络8 小时前
Stable Diffusion 视觉大模型微调技术深度调研
算法
复杂网络8 小时前
基于 Stable Diffusion 架构的视觉大模型代表性工作与原理深度解析
算法
MrZhao4008 小时前
Agent Loop 如何用 Hook 扩展:权限、日志与工具拦截
算法
MrZhao4008 小时前
Agent 为什么需要 Skills:别把所有知识都塞进 system prompt
算法
兵慌码乱1 天前
基于 MediaPipe 与 PySide2 的手势交互音乐控制系统实现:轻量化视觉交互全流程解析
python·opencv·计算机视觉·人机交互·手势识别·mediapipe·pyside2