刷题日志 | Leetcode Hot 100 哈希

【个人刷题记录】因为鼠鼠还在实习,所以基本一天4题左右。两题错题+两题新题,我真的不想刷了就忘了😭

每写完一个板块就会记录一下(未来二刷三刷如果有新的体会都会更新),自我监督,让自己刷的有动力~大家一起加油🎉

1. 哈希

1.1 两数之和

两种方法解决,暴力 or 字典

python 复制代码
class Solution(object):
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        # 暴力法
        # i=0
        # j=1
        # length=len(nums)
        # for i in range(length):
        #     for j in range(i+1,length):
        #         res=nums[i]+nums[j]
        #         if res==target:
        #             return [i,j]
                

        # 字典法
        pool={}
        length=len(nums)
        for i in range(length):
            derta=target-nums[i]
            goal=pool.get(derta,None)
            if goal!=None:
                return [i,goal]

            pool[nums[i]]=i

1.2 字母异位词分组

语法沉淀:

  1. str(sorted(str)):排序str的不二选择
  2. list(dic.values()):直接返回字典的value列表
  3. target not in dic:直接查找是否存在字典中对应的key
python 复制代码
class Solution(object):
    def groupAnagrams(self, strs):
        """
        :type strs: List[str]
        :rtype: List[List[str]]
        """
        pool={} # str-list
        for item in strs:
            sorted_str=str(sorted(item))
            if sorted_str not in pool:
                pool[sorted_str]=[item]
            else:
                pool[sorted_str].append(item)

        return list(pool.values())

下面这种方法也可以学学:

collections.defualtdict(list):直接定义value的类型,这样就不用if-else分情况讨论了,直接添加就行

python 复制代码
from collections import defaultdict
class Solution(object):
    def groupAnagrams(self, strs):
        """
        :type strs: List[str]
        :rtype: List[List[str]]
        """
        dic=defaultdict(list)
        for s in strs:
            key=str(sorted(s))
            dic[key].append(s)
        return list(dic.values())

1.3 最长连续序列

解法1:排序后双指针

语法沉淀:

  1. set:无法索引。想要解决,可以转为list
  2. sorted():才能返回一个新的列表。.sort()是原地排序,无法转为新的列表
python 复制代码
class Solution(object):
    def longestConsecutive(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        # 解法1:排序后遍历验证
        nums_set=set(nums)
        sorted_nums=sorted(list(nums_set))
        max_len=0
        length=len(sorted_nums)
        l=0
        r=0
        while l < length:
            cur=sorted_nums[l]
            r=l+1
            while r<length and sorted_nums[r]==cur+1:
                cur+=1
                r+=1
            
            max_len=max(max_len,r-l)
            l=r

        return max_len

解法2:集合(底层使用哈希实现)

这里的妙点在于 找到最小 往后推进

python 复制代码
class Solution(object):
    def longestConsecutive(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        # 从最小的出发,查看累加值是否在集合中
        # set 底层 是哈希
        s=set(nums)
        max_len=0
        for item in s:
            if item-1 not in s:
                x=item

                while x+1 in s:
                    x+=1
                max_len=max(max_len,x-item+1)
        return max_len
相关推荐
信奥卷王15 小时前
[GESP202609 六级] 分树规划
算法
夏幻灵15 小时前
LeetCode 21:合并两个有序链表——双指针与哑节点详解
算法·leetcode·链表
徐小黑ACG16 小时前
Golang 基础05 结构体struct
开发语言·算法·golang
Navigator_Z17 小时前
LeetCode //C - 1286. Iterator for Combination
c语言·算法·leetcode
北极有牛17 小时前
cpp学习笔记--常量指针
java·开发语言·算法
GPU实战笔记17 小时前
云端 GPU 训练的账户余额提醒:它能说明什么,不能说明什么?
深度学习·算法·成本管理·gpu云计算·云端训练
浮链序18 小时前
怎么证明"你这个模型是偷我的"——把蒸馏变成取证工具
算法·安全·llm
小O的算法实验室18 小时前
IEEE TMC,面向异构物联网的多目标量子行为拓扑优化算法
算法
圣保罗的大教堂18 小时前
leetcode 301. 删除无效的括号 困难
leetcode