题目
解法1:set
这种解法比较巧妙的运用了set和&操作,但速度挺慢
class Solution:def peopleIndexes(self, favoriteCompanies: List[List[str]]) -> List[int]:A = favoriteCompaniesdic = {
tuple(x):ind for ind,x in enumerate(A)}A.sort(key = lambda x:len(x))ans = []for i in range(len(A)):flag = Falsefor j in range(i+1,len(A)):if set(A[i]) & set(A[j]) == set(A[i]):flag = Truebreakif not flag:ans.append(dic[tuple(A[i])])ans.sort()return ans
解法2:bitwise
未完待续
参考:
https://leetcode.com/problems/people-whose-list-of-favorite-companies-is-not-a-subset-of-another-list/discuss/919416/Python-bitwise-with-detailed-explanation