刷题日志 | 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
相关推荐
aaaameliaaa39 分钟前
字符函数和字符串函数
c语言·笔记·算法
城管不管2 小时前
ReAct、Plan-and-Execute、Reflection 三大智能 Agent 范式核心区别
java·人工智能·算法·spring·ai·动态规划
月疯2 小时前
二分法算法(水平等分图形面积)
算法
豆瓣鸡2 小时前
算法日记 - Day3
java·开发语言·算法
白白白小纯2 小时前
算法篇—反转链表
c语言·数据结构·算法·leetcode
Achou.Wang3 小时前
深入理解go语言-第5章 并发编程——Go的灵魂
大数据·算法·golang
The Chosen One9853 小时前
高进度算法模板速记(待完善)
java·前端·算法
圣保罗的大教堂5 小时前
leetcode 3517. 最小回文排列 I 中等
leetcode
土豆.exe5 小时前
Fastjson2 2.0.53 哈希碰撞 RCE:从原理到三种打法
算法·哈希算法
黄河123长江6 小时前
有限Abel群的结构()
算法