标签:highlight char break imu str turn append int max
用了python的set。为了效率,先做了预处理,并排序了。要注意,排序完才好预处理,否则i,j会对不上。
class Solution: def maxProduct(self, words: List[str]) -> int: maxProd = 0 words = sorted(words, key=lambda x:-len(x)) charSets = [] for i in range(len(words)): charSets.append(set(list(words[i]))) for i in range(len(words)): for j in range(i + 1, len(words)): if len(charSets[i] & charSets[j]) == 0: prod = len(words[i]) * len(words[j]) if prod > maxProd: maxProd = prod break return maxProd
[leetcode]Maximum Product of Word Lengths
标签:highlight char break imu str turn append int max
原文地址:https://www.cnblogs.com/lautsie/p/12249819.html