哈希表
考点:
class Solution: def firstUniqChar(self, s: str) -> str: ch_dict = {} for i in range(len(s)): cur = s[i] if cur in s[:i]: continue if not cur in s[i+1:]: return cur return " "
考点:
class Solution: def findRepeatNumber(self, nums: List[int]) -> int: n = len(nums) nums.sort() for i in range(1, n): if nums[i] == nums[i-1]: return nums[i]
class Solution: def groupAnagrams(self, strs: List[str]) -> List[List[str]]: result_dict = collections.defaultdict(list) for istr in strs: istr_list = list(istr) istr_list.sort() # print("".join(istr_list)) result_dict["".join(istr_list)].append(istr) result = [] for _, values in result_dict.items(): result.append(values) return result
class WordsFrequency: def __init__(self, book: List[str]): self.word_fre = {} for word in book: if word in self.word_fre: self.word_fre[word] = self.word_fre[word] + 1 else: self.word_fre[word] = 1 def get(self, word: str) -> int: return self.word_fre.get(word, 0)
5、剑指 Offer 48. 最长不含重复字符的子字符串
考点:
1、注意双指针的序列长度要>=2,等于0和1 需要使用特殊值处理
2、for 循环right指针,range范围是(1, len(s)+1)
class Solution: def lengthOfLongestSubstring(self, s: str) -> int: if len(s) == 0: return 0 if len(s) == 1: return 1 result = 0 left = 0 for right in range(1, len(s)+1): # print(s[left:right]) # 不重复 if len(set(s[left:right])) == right - left: if right - left > result: result = right - left continue # 重复 while True: left = left + 1 if left == right: break if len(set(s[left:right])) == right - left: if right - left > result: result = right - left break return result
6、726. 原子的数量