标签:
Example 1:
Given [“abcw”, “baz”, “foo”, “bar”, “xtfn”, “abcdef”]
Return 16
The two words can be “abcw”, “xtfn”.
Example 2:
Given [“a”, “ab”, “abc”, “d”, “cd”, “bcd”, “abcd”]
Return 4
The two words can be “ab”, “cd”.
Example 3:
Given [“a”, “aa”, “aaa”, “aaaa”]
Return 0
No such pair of words.
current_word = set(word[i])
for word in words:
for char in current_word:
if char in words:
break
else:
result = max(len(current_word)*len(word),result)
class Solution(object):
def maxProduct(self, words):
result=0
while words:
current_word = set(words[0])
current_lentghe = len(words[0])
words = words[1:] #对原向量进行变换
for word in words:
for char in current_word:
if char in word:
break
else:
result = max(result, current_length*len(word))
return result
abc=00 0000 0000 0000 0000 0000 0111
xyz=11 1000 0000 0000 0000 0000 0000
对于python,可使用:
num[i] = sum(1 << (ord(x)-ord(‘a’)) for x in set(words[i]))
创建26位数组,其中 1<
class Solution(object):
def maxProduct(self, words):
nums = []
size = len(words)
for w in words:
nums += sum(1 << (ord(x) - ord(‘a’)) for x in set(w)),
ans = 0
for x in range(size):
for y in range(size):
if not (nums[x] & nums[y]):
ans = max(len(words[x]) * len(words[y]), ans)
return ans
Leetcode:Maximum Product of Word Lengths
标签:
原文地址:http://blog.csdn.net/neoye125/article/details/51364487