【力扣hot100】刷题笔记Day2

前言

  • hot100!开刷!不熟悉python就cv大法先,理清楚思路更重要

哈希

1. 两数之和 - 力扣(LeetCode)

  • 暴力法

    • 能过,遍历两遍求和是否为target
    • 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]
              return []
  • 哈希法

    • 遍历一遍,每次查找表里是否有target减当前值
    • class Solution(object):
          def twoSum(self, nums, target):
              hashtable = {}  # 哈希表
              for i, num in enumerate(nums):
                  if target - num in hashtable:  # 如果差值在哈希表里
                      return [hashtable[target-num], i]  # 返回结果
                  hashtable[nums[i]] = i  # 加入哈希表
              return []

49. 字母异位词分组 - 力扣(LeetCode)

  • 排序法

    • 对字符串排序(记得重新组合),记录到哈希表里(value是列表)
    • class Solution(object):
          def groupAnagrams(self, strs):
              # mp = collections.defaultdict(list)  
              # 哈希表,key为任意值,value为列表,默认为空列表
              # 访问mp中不存在的键时,defaultdict将自动为该键创建一个空列表作为值,从而避免KeyError异常
              mp = {}
              for st in strs:
                  key = "".join(sorted(st))  # sort之后会返回字符列表,组合后才是字符串
                  # 用defaultdict这一句if就可以不用
                  if key not in mp:
                      mp[key] = []
                  mp[key].append(st)
              return list(mp.values())
  •  计数法

    • 字母异位词对应的字母数量一定是一样的,也可以用哈希,记得转成元组才能哈
    • class Solution:
          def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
              mp = collections.defaultdict(list)  # 不用判空
              for st in strs:  # 遍历每个字符串
                  counts = [0] * 26  # 长度26值为0的数组
                  for ch in st:   # 遍历每一个字符
                      counts[ord(ch) - ord("a")] += 1  # 统计字符串字符个数
                  # 需要将 list 转换成 tuple 才能进行哈希
                  mp[tuple(counts)].append(st)
              return list(mp.values())

128. 最长连续序列 - 力扣(LeetCode)

  • 排序+滑动窗口
    • set去重、排序之后再从头遍历,记录连续最长长度,sort就O(nlogn),不符合要求
    • class Solution(object):
          def longestConsecutive(self, nums):
              if not nums:  # 如果数组为空,直接返回0
                  return 0
              num_set = set(nums)  # 去重
              num_list = sorted(list(num_set))  # 排序
              res= 1  # 最长连续序列至少为1
              cur_res= 1  # 当前连续序列长度初始化为1
              for i in range(1, len(num_list)):
                  if num_list[i] - num_list[i - 1] == 1:  # 如果当前数字与前一个数字连续
                      cur_res+= 1  # 则增加当前连续序列长度
                  else:
                      res= max(res, cur_res)  # 否则,重置当前连续序列长度为1
                      cur_res= 1
              # 循环结束后,还需要再比较一次,因为最长连续序列可能出现在数组末尾
              res= max(res, cur_res)
              return res
  • 哈希法

    • set去重,遍历每个数,只要数减一不在,就查找连续增加值是否在set里,更新长度,O(n)
    • class Solution(object):
          def longestConsecutive(self, nums):
              res = 0  # 记录结果长度,空为0
              num_set = set(nums) # 用set进行去重
              for num in num_set:  # 挨个遍历set
                  if num - 1 not in num_set:  # 保证从最小的开始
                      cur_num = num  # 记录当前数
                      cur_res = 1  # 初始化序列长度为1
                      while cur_num + 1 in num_set:  # 只要找到连续大的数
                          cur_num += 1  # 找下一个数
                          cur_res += 1  # 长度+1
                      res = max(res, cur_res)  # 记录最长串
              return res

后言

  • 家教回来刷个题就深夜了,晚上安静刷题确实爽啊,开个好头,睡了睡了 

你可能感兴趣的:(力扣hot100刷题笔记,leetcode,笔记,算法,职场和发展,数据结构,力扣)