leetcode -- Maximum Product of Word Lengths -- 重点

https://leetcode.com/problems/maximum-product-of-word-lengths/

主要思路:http://www.jianshu.com/p/bb84b0f866c9

用26 bit 的int来convert a word to a bit number.

最终solution还是O( n2 )的

code参考:http://bookshadow.com/weblog/2015/12/16/leetcode-maximum-product-word-lengths/

class Solution(object):
    def maxProduct(self, words):
        """ :type words: List[str] :rtype: int """
        nums = []
        size = len(words)
        for w in words:
            nums += sum(1 << (ord(x) - ord('a')) for x in set(w))#这里就是把a存到第一位,把z存到第26位
        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)