deftwoSum(nums, target): hashmap = {} for index, num inenumerate(nums): another_num = target - num if another_num in hashmap: return [hashmap[another_num], index] hashmap[num] = index returnNone
defintersection1(nums1, nums2): # 使用哈希映射 dic = {} for i in nums1: ifnot dic.get(i): dic[i]=1 new = [] for i in nums2: if dic.get(i): new.append(i) dic[i] -= 1 return new
defisHappy(n): n = str(n) visited = set() while1: n = str(sum(int(i) ** 2for i in n)) if n == "1": returnTrue if n in visited: returnFalse visited.add(n)
deffindWords1(words): l1 = 'qwertyuiopQWERTYUIOP' l2 = 'asdfghjklASDFGHJKL' l3 = 'zxcvbnmZXCVBNM' l1,l2,l3 = set(l1),set(l2),set(l3) final = [] for i inrange(len(words)): s = set(words[i]) if s&l1 == s or s&l2==s or s&l3==s: final.append(words[i]) return final
deffindWords2(words): set1 = set('qwertyuiop') set2 = set('asdfghjkl') set3 = set('zxcvbnm') res = [] for i in words: x = i.lower() setx = set(x) if setx<=set1 or setx<=set2 or setx<=set3: res.append(i) return res
defcontainsNearbyDuplicate1(nums, k): # 题目中差值最大为k, 意思是只要小于等于k则为True, 而不是所有差值的最大值小于等于k # 将相同值的索引存储起来 import collections d = collections.defaultdict(list) for i, v inenumerate(nums): d[v].append(i) # 对索引值进行差值的判断, 小于等于k, 则返回True for v in d.values(): for i inrange(len(v) - 1): if v[i + 1] - v[i] <= k: returnTrue returnFalse
defcontainsNearbyDuplicate2(nums, k): lookup = {} # 没有重复元素, 肯定返回False iflen(set(nums)) == len(nums): returnFalse # 只要有一个重复元素 相差的索引号 <=k 返回True for idx, num inenumerate(nums): if num in lookup andabs(idx - lookup[num]) <= k: returnTrue lookup[num] = idx returnFalse
deffindTheDifference1(s, t): res = {} for i in s: if res.get(i): res[i] += 1# 记录s中的字符出现次数 else: res[i] = 1 for i in t: if res.get(i): # 消除字典中存在的字符 res[i] -= 1 else: return i # 字典中没有相同字符,则返回该字符
deffindTheDifference2(s, t): # 字符与ASCII码值转换 a = 0 b = 0 for i in s: a += ord(i) for i in t: b += ord(i) returnchr(b-a)
deffindLHS(nums): hashmap = {} for item in nums: if item notin hashmap: hashmap[item] = 1 else: hashmap[item] += 1 res = 0 for item in hashmap: if item+1in hashmap: up = hashmap[item+1] res = max(res,up+hashmap[item]) return res
defdistributeCandies1(candies): # 将糖果种类的数字值作为 key 存储在字典中,糖果一旦存在则进行标记,并将糖果种类加一 length = len(candies) each = length // 2 type_dict = dict() type_cnt = 0 for c in candies: if type_dict.get(c, 0) == 0: type_dict[c] = 1 type_cnt += 1 if type_cnt >= each: return each else: return type_cnt
defdistributeCandies2(candies): # 利用集合无重复元素的特性,将数据存入集合,从而求出糖果的种类。 each = len(candies) // 2 candies_set = set(candies) return each if each <= len(candies_set) elselen(candies_set)
给定两个字符串 s 和 t,判断它们是否是同构的。 如果 s 中的字符可以被替换得到 t ,那么这两个字符串是同构的。 所有出现的字符都必须用另一个字符替换,同时保留字符的顺序。两个字符不能映射到同一个字符上,但字符可以映射自己本身。
示例 1: 输入: s = “egg”, t = “add” 输出: true
示例 2: 输入: s = “foo”, t = “bar” 输出: false
示例 3: 输入: s = “paper”, t = “title” 输出: true
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
defisIsomorphic(s, t): iflen(s) != len(t): # 如果两个字符串的长度不一致,则不同构 returnFalse # 用字典来记录每个字母最后出现的位置,判断s和t中每个字母最后出现的位置是否相同,不相同则不同构 last_a ,last_b= {},{} for i inrange(len(s)): if s[i] notin last_a: last_a[s[i]] = i if t[i] notin last_b: last_b[t[i]] = i if last_a[s[i]] != last_b[t[i]]: returnFalse last_a[s[i]] = i last_b[t[i]] = i returnTrue