LeetCode Hot100 刷题笔记(1)—— 哈希、双指针、滑动窗口

目录

前言

一、哈希

1.两数之和

[2. 字母异位词分组](#2. 字母异位词分组)

[3. 最长连续序列](#3. 最长连续序列)

二、双指针

[1. 移动零](#1. 移动零)

[2. 盛最多水的容器](#2. 盛最多水的容器)

[3. 三数之和](#3. 三数之和)

[4. 接雨水](#4. 接雨水)

三、滑动窗口

[1. 无重复字符的最长子串](#1. 无重复字符的最长子串)

[2. 找到字符串中所有字母异位词](#2. 找到字符串中所有字母异位词)


前言

一、哈希:两数之和,字母异位词分组,最长连续序列。

二、双指针:移动零,盛最多水的容器,三数之和,接雨水。

三、滑动窗口:无重复字符的最长子串,找到字符串中所有字母异位词。


一、哈希

1.两数之和

原题链接:1. 两数之和 - 力扣(LeetCode)

python 复制代码
class Solution(object):
    def twoSum(self, nums, target):
        n = len(nums)
        for i in range(n):
            for j in range(i+1, n):
                if nums[i] + nums[j] == target:
                    return [i, j]

2. 字母异位词分组

原题链接:49. 字母异位词分组 - 力扣(LeetCode)

python 复制代码
class Solution(object):
    def groupAnagrams(self, strs):
        res = []
        dicts = {}
        for s in strs:
            s_ = ''.join(sorted(s))
            if s_ in dicts:
                dicts[s_].append(s)
            else:
                dicts[s_] = [s]
        return list(dicts.values())

3. 最长连续序列

原题链接:128. 最长连续序列 - 力扣(LeetCode)

python 复制代码
class Solution(object):
    def longestConsecutive(self, nums):
        # left, right双指针
        res = 0
        nums = set(nums)
        for left in nums:
            if left-1 not in nums:    # 找片段左端点left
                right = left + 1
                while right in nums:  # 找片段右端点right
                    right += 1
                res = max(res, right-left)
        return res

二、双指针

1. 移动零

原题链接:283. 移动零 - 力扣(LeetCode)

python 复制代码
class Solution(object):
    def moveZeroes(self, nums):
        for i, n in enumerate(nums):
            if n == 0:
                nums.remove(n)
                nums.append(n)
        return nums

2. 盛最多水的容器

原题链接:11. 盛最多水的容器 - 力扣(LeetCode)

python 复制代码
# 考点:左右指针(left & right)
class Solution(object):
    def maxArea(self, height):
        n = len(height)
        left, right = 0, n-1
        res = 0
        while left < right:
            area = min(height[left], height[right]) * (right - left)
            res = max(res, area)
            if height[left] <= height[right]:
                left +=1
            else:
                right -=1
        return res

3. 三数之和

原题链接:15. 三数之和 - 力扣(LeetCode)

python 复制代码
# 考点:左右指针(left & right)
class Solution(object):
    def threeSum(self, nums):
        nums.sort()
        if min(nums) > 0:
            return []
        n = len(nums)
        res = []

        for i in range(n):
            if i>0 and nums[i] == nums[i-1]:
                continue
            left, right = i+1, n-1
            while left < right:
                if nums[i]+nums[left]+nums[right] == 0:
                    while left < right and nums[left] == nums[left+1]:
                        left += 1
                    while left < right and nums[right] == nums[right-1]:
                        right -= 1
                    res.append([nums[i], nums[left], nums[right]])
                    left += 1
                    right -= 1
                elif nums[i]+nums[left]+nums[right] < 0:
                    left += 1
                else:
                    right -= 1
        return res

4. 接雨水

原题链接:42. 接雨水 - 力扣(LeetCode)

python 复制代码
class Solution(object):
    def trap(self, height):
        # prefix_max最大前缀, suffix_max最大后缀, min(prefix_max, suffix_max) - h
        res = 0
        n = len(height)

        prefix_max = [0] * n
        prefix_max[0] = height[0]
        for i in range(1, n):
            prefix_max[i] = max(prefix_max[i-1], height[i])
        
        suffix_max = [0] * n
        suffix_max[-1] = height[-1]
        for i in range(n-2, -1, -1):
            suffix_max[i] = max(height[i], suffix_max[i+1])
        
        for h, prefix, suffix in zip(height, prefix_max, suffix_max):
            res = res + min(prefix, suffix) - h
        return res

三、滑动窗口

1. 无重复字符的最长子串

原题链接:3. 无重复字符的最长子串 - 力扣(LeetCode)

python 复制代码
# 考点:滑窗(不定窗口),快慢指针
class Solution(object):
    def lengthOfLongestSubstring(self, s):
        from collections import Counter
        cnt = Counter()
        left = 0 
        res = 0
        for right, c in enumerate(s):
            cnt[c] += 1
            while cnt[c] > 1:
                cnt[s[left]] -= 1
                left += 1
            res = max(res, right-left+1)
        return res

2. 找到字符串中所有字母异位词

原题链接:438. 找到字符串中所有字母异位词 - 力扣(LeetCode)

python 复制代码
class Solution(object):
    def findAnagrams(self, s, p):
        # 考点:滑窗(固定窗口),快慢指针
        from collections import Counter
        left = 0
        res = []
        cnt_p = Counter(p)
        cnt_s = Counter()
        for right, c in enumerate(s):
            cnt_s[c] += 1
            while right - left + 1 == len(p):
                if cnt_s == cnt_p:
                    res.append(left)
                if s[left] in cnt_s:
                    cnt_s[s[left]] -= 1
                if cnt_s[s[left]] == 0:
                    del cnt_s[s[left]]
                left += 1
        return res
相关推荐
_Kayo_2 小时前
node.js 学习笔记3 HTTP
笔记·学习
星星火柴9366 小时前
关于“双指针法“的总结
数据结构·c++·笔记·学习·算法
Cx330❀8 小时前
【数据结构初阶】--排序(五):计数排序,排序算法复杂度对比和稳定性分析
c语言·数据结构·经验分享·笔记·算法·排序算法
小幽余生不加糖9 小时前
电路方案分析(二十二)适用于音频应用的25-50W反激电源方案
人工智能·笔记·学习·音视频
..过云雨9 小时前
01.【数据结构-C语言】数据结构概念&算法效率(时间复杂度和空间复杂度)
c语言·数据结构·笔记·学习
岑梓铭10 小时前
考研408《计算机组成原理》复习笔记,第五章(3)——CPU的【数据通路】
笔记·考研·408·计算机组成原理·计组
一匹电信狗16 小时前
【C++】异常详解(万字解读)
服务器·c++·算法·leetcode·小程序·stl·visual studio
Blossom.11816 小时前
把 AI 推理塞进「 8 位 MCU 」——0.5 KB RAM 跑通关键词唤醒的魔幻之旅
人工智能·笔记·单片机·嵌入式硬件·深度学习·机器学习·搜索引擎
草莓熊Lotso17 小时前
《吃透 C++ 类和对象(中):const 成员函数与取地址运算符重载解析》
c语言·开发语言·c++·笔记·其他
墨染点香17 小时前
LeetCode 刷题【43. 字符串相乘】
算法·leetcode·职场和发展