In this chapter, there are several problems that are rather straightforward and possess numerous approaches. As a result, those questions have been omitted herein.
Group Anagrams
Group Anagramshttps://leetcode.cn/problems/group-anagrams/
Difficulty: MED
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
anagrams = {}
for s in strs:
iden = "".join(sorted(s))
if iden in anagrams:
anagrams[iden].append(s)
else:
anagrams[iden] = [s]
return list(anagrams.values())
Longest Consecutive Sequence
Longest Consecutive Sequencehttps://leetcode.cn/problems/longest-consecutive-sequence/Difficulty: MED
class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
# searching in set O(1)
# searching in list O(n)
res = 0
set_nums = set(nums)
for num in set_nums:
if num - 1 not in set_nums:
start = num
while num in set_nums:
num += 1
res = max(res, num - start)
return res
标签:150,iden,set,anagrams,nums,res,Top,num,Interview From: https://blog.csdn.net/gcsyymm/article/details/145041664